Golang Gin 优雅地解析JSON请求数据(ShouldBindBodyWith避免出现EOF错误)

avtion · · 16207 次点击 · 开始浏览    置顶
这是一个创建于 的主题,其中的信息可能已经有所发展或是发生改变。

> `JSON`是前后端交互的重要数据类型之一,使用`Gin Web框架`可以很方便地将HTTP请求报文中`JSON`格式的`Body`数据解析到`结构体Struct`或`字典Map`数据结构中。 环境 ``` go version go1.14.3 windows/amd64 github.com/gin-gonic/gin v1.6.3 ``` ## 1. 结论 > 参考 [Fix #216: Enable to call binding multiple times in some formats #1341](https://github.com/gin-gonic/gin/pull/1341) ![image.png](https://static.studygolang.com/200518/456d3cef308aa900384683d4568cf62f.png) `ShouldBindJSON`方法是最常用解析JSON数据的方法之一,但在重复调用的情况下会出现`EOF`的报错,这个原因出在`ShouldBindJSON`在调用过一次之后`context.request.body.sawEOF`的值是`false`导致,所以如果要多次绑定多个变量,需要使用`ShouldBindBodyWith`。 至于为什么单次绑定不优选使用`BindJSON`方法,主要因为`BindJSON`方法会强制抛出错误,影响正常流程。 以下为范例: ```go // 以下是用于存储JSON数据的结构体 type MsgJson struct { Msg string `json:"msg"` } // 单次绑定使用 ShouldBindJSON 方法 func bindExample(c *gin.Context) { // ---> 声明结构体变量 var a MsgJson // ---> 绑定数据 if err := c.ShouldBindJSON(&a); err != nil { c.AbortWithStatusJSON( http.StatusInternalServerError, gin.H{"error": err.Error()}) return } // --> 返回 c.JSON(http.StatusOK, gin.H{"msg": "ok"}) return } // 多次绑定优先使用 ShouldBindBodyWith 方法 func bindWithRightWay(c *gin.Context) { // ---> 声明两个结构体变量用于存储JSON数据 var a, b MsgJson // ---> 第一次解析(注意第二个参数是 binding.JSON) if err := c.ShouldBindBodyWith(&a, binding.JSON); err != nil { c.AbortWithStatusJSON( http.StatusInternalServerError, gin.H{"error": err.Error()}) return } // ---> 第二次解析 if err := c.ShouldBindBodyWith(&b, binding.JSON); err != nil { c.AbortWithStatusJSON( http.StatusInternalServerError, gin.H{"error": err.Error()}) return } // ---> 返回 c.JSON(http.StatusOK, gin.H{"msg": "ok"}) return } ``` --- ## 2. EOF错误复现 `EOF`错误出现在第二次使用`ShouldBindJSON`方法,在多次绑定的情况下,优先使用`ShouldBindBodyWith`,以下为❌错误示范: ```go type MsgJson struct { Msg string `json:"msg"` } func main() { r := gin.Default() r.POST("/v2", bindWithError) _ = r.Run("127.0.0.1:9001") } func bindWithError(c *gin.Context) { var a, b MsgJson if err := c.ShouldBindJSON(&a); err != nil {....} // ---> 注意,这里会出现EOF报错 if err := c.ShouldBindJSON(&b); err != nil {....} ....... return } ``` `Postman`测试结果: ![postman测试结果](https://img-blog.csdnimg.cn/20200518233000833.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3llczE2OXllczEyMw==,size_16,color_FFFFFF,t_70) `Goland`断点调试: ![goland断点调试](https://img-blog.csdnimg.cn/20200518233016512.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3llczE2OXllczEyMw==,size_16,color_FFFFFF,t_70) ![request.body.sawEOF](https://img-blog.csdnimg.cn/20200518233033204.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3llczE2OXllczEyMw==,size_16,color_FFFFFF,t_70) --- ## 3. `ShouldBindBodyWith` 源码分析 `ShouldBindBodyWith`和`ShouldBindWith`很像,但它保存了`requests`的`Body`到上下文,允许`Body`被继续调用。 注意:这个方法会先读取`Body`然后绑定,如果只绑定一次,建议使用`ShouldBindWith`来获得更好的性能(因为后者会直接读取并写到指定变量,而没有写入上下文)。 ```go func (c *Context) ShouldBindBodyWith(obj interface{}, bb binding.BindingBody) (err error) { var body []byte // ---> 先看上下文是否已经有Body的Bytes数据 if cb, ok := c.Get(BodyBytesKey); ok { if cbb, ok := cb.([]byte); ok { body = cbb } } // ---> 如果Body不为空的情况下,读取Body数据并写入上下文 if body == nil { body, err = ioutil.ReadAll(c.Request.Body) if err != nil { return err } c.Set(BodyBytesKey, body) } return bb.BindBody(body, obj) } ``` 流程图 ![image.png](https://static.studygolang.com/200518/6a92b57c90d594b5b6aa268b40ed8a14.png)

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

16207 次点击  ∙  1 赞  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传