当我使用Gin时,c.shouldBindJson无法正常工作。我在邮递员中测试我的REST API并获得错误响应
请求
{
Username: "asdasd",
Password: "asdasdasd",
Email: "123@123.com"
}
响应: 邮递员给了我答复
{
"code": 400,
"error": "invalid character 'U' looking for beginning of value",
}
这是数据结构
type WebRegisterData struct {
Username string `json:"username"`
Password string `json:"password"`
Email string `json:"email"`
}
这是相关功能
func (h *Handler) WebRegister(c *gin.Context) {
if h.db == nil {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"error": nil,
})
c.Abort()
return
}
var webRegisterData request.WebRegisterData
err := c.ShouldBindJSON(&webRegisterData)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"error": err.Error(),
})
c.Abort()
return
}
...
}
答案 0 :(得分:0)
您的请求具有无效的JSON格式。
您的请求应如下所示:
{
"username": "asdasd",
"password": "asdasdasd",
"email": "123@123.com"
}