Browse Source

Set content-type to GET requests, fixed ToJsonAll returning invalid json

main
Unbewohnte 3 years ago
parent
commit
81207681fc
  1. 4
      dbHandle/handle.go
  2. 16
      randomData/randomData.go

4
dbHandle/handle.go

@ -155,13 +155,14 @@ func (db *DB) HandleGlobalWeb(w http.ResponseWriter, r *http.Request) {
log.Printf("Could not retrieve db contents: %s\n", err) log.Printf("Could not retrieve db contents: %s\n", err)
return return
} }
randomDatasJsonBytes, err := randomdata.ToJsonAll(randomDatas, true) randomDatasJsonBytes, err := randomdata.ToJsonAll(randomDatas)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Printf("Could not convert to json: %s\n", err) log.Printf("Could not convert to json: %s\n", err)
return return
} }
w.Header().Add("content-type", "application/json")
w.Write(randomDatasJsonBytes) w.Write(randomDatasJsonBytes)
default: default:
@ -197,6 +198,7 @@ func (db *DB) HandleSpecificWeb(w http.ResponseWriter, r *http.Request) {
return return
} }
w.Header().Add("content-type", "application/json")
w.Write(rdJsonBytes) w.Write(rdJsonBytes)
case http.MethodDelete: case http.MethodDelete:

16
randomData/randomData.go

@ -35,18 +35,10 @@ func (rd *RandomData) ToJson() ([]byte, error) {
} }
// Convert all rdatas to []byte // Convert all rdatas to []byte
func ToJsonAll(rdatas []*RandomData, addNewlines bool) ([]byte, error) { func ToJsonAll(rdatas []*RandomData) ([]byte, error) {
var rdatasBytes []byte rdatasBytes, err := json.Marshal(&rdatas)
for _, rdata := range rdatas { if err != nil {
jsonBytes, err := rdata.ToJson() return nil, err
if err != nil {
return nil, err
}
rdatasBytes = append(rdatasBytes, jsonBytes...)
if addNewlines {
rdatasBytes = append(rdatasBytes, 10)
}
} }
return rdatasBytes, nil return rdatasBytes, nil

Loading…
Cancel
Save