fix (middleware): bodyparser must not return an error if no body

This commit is contained in:
Mickael Kerjean 2022-09-12 01:05:44 +10:00
parent bd9deb858d
commit 0acf94ce0c

View file

@ -11,12 +11,15 @@ import (
func BodyParser(fn func(*App, http.ResponseWriter, *http.Request)) func(ctx *App, res http.ResponseWriter, req *http.Request) {
extractBody := func(req *http.Request) (map[string]interface{}, error) {
var body map[string]interface{}
body := map[string]interface{}{}
byt, err := ioutil.ReadAll(req.Body)
if err != nil {
return body, err
}
if err := json.Unmarshal(byt, &body); err != nil {
if len(byt) == 0 {
err = nil
}
return body, err
}
return body, nil