2019-04-19 10:24
浏览 636


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.

图片转代码服务由CSDN问答提供 功能建议

  import“ github.com/gin-gonic/gin"

func Receive(c * gin  .Context){
 //获取JSON ecoded数据
 rawData,err:= c.GetRawData()
如果err!= nil {
 logger.Info(“ 收到的原始数据-“,rawData)

当我传递Json对象 {” key“:” value“}时,此代码段有效 ,但给出错误-“ JSON输入的意外结尾” ,当我传递类似- [{“ key”:“ val”},{“ key”:“ val“}] 作为输入。


  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dpdfh60088 2019-04-19 10:43

    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
    解决 无用
    打赏 举报

相关推荐 更多相似问题