关于go:Gin-绑定参数

9次阅读

共计 611 个字符,预计需要花费 2 分钟才能阅读完成。

Gin 提供了两类绑定办法:Must bind 和 Should bind

Must bind 的办法有 Bind,BindJSON,BindXML,BindQuery,BindYAML,这些办法属于 BindWith 的具体调用。

Must bind 如果产生绑定谬误,则申请终止,并触发 c.AbortWithError(400, err).SetType(ErrorTypeBind)。响应状态码被设置为 400 并且 Content-Type 被设置为 text/plain; charset=utf-8。如果您在此之后尝试设置响应状态码,Gin 会输入日志 [GIN-debug] [WARNING] Headers were already written. Wanted to override status code 400 with 422。

Should bind 的办法有 ShouldBind,ShouldBindJSON,ShouldBindXML,ShouldBindQuery,ShouldBindYAML,这些办法属于 ShouldBindWith 的具体调用。

Should bind 如果产生绑定谬误,Gin 会返回谬误并由开发者处理错误和申请。
应用 Bind 办法时,Gin 会尝试依据 Content-Type 推断如何绑定。如果你明确晓得要绑定什么,能够应用 MustBindWith 或 ShouldBindWith。

参考:
https://icode.best/i/97680946…

正文完
 0