I'm getting a weird error which previously didn't appear for my code, which might be caused by my JWT
implementation that I didn't test thoroughly tbh. So I have this current code:
ginGine.POST("/edge_device_info", func(ctx *gin.Context) {
reqMap := helpers.GetJSONFromReqBody(ctx.Request.Body, ctx)
accessToken := fmt.Sprintf("%v", reqMap["access_token"])
tokenValid, jwtErr := srvHelpers.ValidateJWTToken(accessToken)
if jwtErr != nil || !tokenValid {
fmt.Println("invalid token")
ctx.JSON(http.StatusBadRequest, gin.H{
"error0": jwtErr.Error(),
})
return
}
// reqBody, err := json.Marshal(reqMap)
// if err != nil {
// ctx.JSON(http.StatusBadRequest, gin.H{
// "error10": err.Error(),
// })
// return
// }
var edge_device_info models.EdgeDeviceInfo
// err = json.Unmarshal(reqBody, &edge_device_info)
err := ctx.ShouldBind(&edge_device_info)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"error1": err.Error(),
})
return
}
// more code
})
This API endpoint gets sent this similar looking json
:
{
"name": "Some text",
"description": "Another bunch of text",
"access_token": "SOME_JWT_TOKEN"
}
Now, this json looks fine, right? But ShouldBind()
doesn't like that and would return an EOF
. If you check the top part of my code, I'm converting the received JSON
body to a map then use the access_token
, which is a JWT
token, to validate the user's access. I'm not quite sure if that process somehow made that issue, and that's what I wanted to know. That's the reason I made this question. I already found the workaround, which is the commented code above. Which is quite weird as creating the map in the first place means that the request body's JSON
was valid.