Skip to content

Instantly share code, notes, and snippets.

@chuprik
Last active April 3, 2019 08:43
Show Gist options
  • Save chuprik/fd3637ab6b41f00a30dd71f9a1597bad to your computer and use it in GitHub Desktop.
Save chuprik/fd3637ab6b41f00a30dd71f9a1597bad to your computer and use it in GitHub Desktop.
golang, echo, application/json request
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
}
@LordNoteworthy
Copy link

There is a bug in : s, err := ioutil.ReadAll(c.Request().Body())
It should be : s, err := ioutil.ReadAll(c.Request().Body)

@baorv
Copy link

baorv commented Jan 15, 2019

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