When I use Gin, c.shouldBindJson is not working correctly. I test my REST API in postman and get the error response
Request
{
Username: "asdasd",
Password: "asdasdasd",
Email: "123@123.com"
}
Response: Postman gives me the response
{
"code": 400,
"error": "invalid character 'U' looking for beginning of value",
}
Here is the data struct
type WebRegisterData struct {
Username string `json:"username"`
Password string `json:"password"`
Email string `json:"email"`
}
Here is the relevant function
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
}
...
}
Your request is of invalid JSON format.
Your request should look like:
{
"username": "asdasd",
"password": "asdasdasd",
"email": "123@123.com"
}