Last active
April 3, 2019 08:43
-
-
Save chuprik/fd3637ab6b41f00a30dd71f9a1597bad to your computer and use it in GitHub Desktop.
golang, echo, application/json request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils | |
import ( | |
"encoding/json" | |
"io/ioutil" | |
"github.com/labstack/echo" | |
) | |
func BodyToJson(c echo.Context) (map[string]interface{}, error) { | |
s, err := ioutil.ReadAll(c.Request().Body()) | |
if err != nil { | |
return nil, err | |
} | |
var body map[string]interface{} | |
if err := json.Unmarshal(s, &body); err != nil { | |
return nil, err | |
} | |
return body, nil | |
} |
There is a bug in : s, err := ioutil.ReadAll(c.Request().Body())
It should be : s, err := ioutil.ReadAll(c.Request().Body)
That's right!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is a bug in : s, err := ioutil.ReadAll(c.Request().Body())
It should be : s, err := ioutil.ReadAll(c.Request().Body)