我正在尝试使用 Gin 将 Base64 图像发送到用 Go 编写的服务器。我创建了一个带有绑定(bind)和 json 标签的结构来表示请求正文,如下所示:
type createCompanyRequestBody struct {
Name string `json:"name" binding:"required"`
Size string `json:"size" binding:"required"`
Logo string `json:"logo" binding:"required,base64|base64url|base64rawurl"`
}
当尝试使用 Gin 的 ShouldBindJSON
解码正文时,我收到 Logo
字段的错误。不过,我确实使用在线工具 ( https://onlinepngtools.com/convert-base64-to-png ) 验证了解码后的对象确实在 Logo
字段中包含有效的 base64 字符串。
解码JSON的代码如下:
var body createCompanyRequestBody
if err := ctx.ShouldBindJSON(&body); err != nil {
ctx.Status(http.StatusBadRequest) // Will always hit this
}
我以前没有用过 Gin ,所以我确信我有疏忽,但我似乎不明白是什么。如何更改结构以允许按预期提供的 base64 变体?
最佳答案
关于去 Gin : Validating base64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76858411/