mirror of
https://github.com/optim-enterprises-bv/databunker.git
synced 2025-11-02 19:07:46 +00:00
make getJSONPostData function to be more generic
This commit is contained in:
20
src/utils.go
20
src/utils.go
@@ -575,8 +575,6 @@ func getJSONPostData(r *http.Request) ([]byte, error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
cType = strings.ToLower(cType)
|
cType = strings.ToLower(cType)
|
||||||
records := make(map[string]interface{})
|
|
||||||
var records2 []map[string]interface{}
|
|
||||||
if r.Method == "DELETE" {
|
if r.Method == "DELETE" {
|
||||||
// otherwise data is not parsed!
|
// otherwise data is not parsed!
|
||||||
r.Method = "PATCH"
|
r.Method = "PATCH"
|
||||||
@@ -601,26 +599,18 @@ func getJSONPostData(r *http.Request) ([]byte, error) {
|
|||||||
if len(form) == 0 {
|
if len(form) == 0 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
records := make(map[string]interface{})
|
||||||
for key, value := range form {
|
for key, value := range form {
|
||||||
records[key] = value[0]
|
records[key] = value[0]
|
||||||
}
|
}
|
||||||
return json.Marshal(records)
|
return json.Marshal(records)
|
||||||
} else if strings.HasPrefix(cType, "application/json") || strings.HasPrefix(cType, "application/xml") {
|
} else if strings.HasPrefix(cType, "application/json") || strings.HasPrefix(cType, "application/xml") {
|
||||||
if body[0] == '{' {
|
var data interface{}
|
||||||
err = json.Unmarshal([]byte(body), &records)
|
err := json.Unmarshal([]byte(body), &data)
|
||||||
} else if body[0] == '[' {
|
|
||||||
err = json.Unmarshal([]byte(body), &records2)
|
|
||||||
} else {
|
|
||||||
return nil, errors.New("wrong content-type, not a json string")
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errors.New("error decoding json data")
|
||||||
}
|
|
||||||
if body[0] == '{' {
|
|
||||||
return json.Marshal(records)
|
|
||||||
} else if body[0] == '[' {
|
|
||||||
return json.Marshal(records2)
|
|
||||||
}
|
}
|
||||||
|
return json.Marshal(data)
|
||||||
}
|
}
|
||||||
log.Printf("Ignore wrong content type: %s", cType)
|
log.Printf("Ignore wrong content type: %s", cType)
|
||||||
maxStrLen := 200
|
maxStrLen := 200
|
||||||
|
|||||||
Reference in New Issue
Block a user