import "github.com/gin-gonic/gin"
func Receive(c *gin.Context) {
// Gets JSON ecnoded data
rawData, err := c.GetRawData()
if err != nil {
return nil, err
}
logger.Info("Raw data received - ", rawData)
}
This code snippet works when I pass a Json object {"key":"value"} but gives an error - "unexpected end of JSON input" when I pass an array like - [{"key":"val"},{"key": "val"}] as the input.
Any help on this would be appreciated.
All GetRawData() does is return stream data, so that shouldn't cause your error:
// GetRawData return stream data.
func (c *Context) GetRawData() ([]byte, error) {
return ioutil.ReadAll(c.Request.Body)
}
However, try using BindJSON
and deserialise into a struct. See for example this question.
type List struct {
Messages []string `key:"required"`
}
func Receive(c *gin.Context) {
data := new(List)
err := c.BindJSON(data)
if err != nil {
return nil, err
}
}