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

16
randomData/randomData.go

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

Loading…
Cancel
Save