-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
improving token config and create bindingJSON utils #7
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your token improvement @huf0813 . But i still don't understand why you need to create that BindingJSON() method. Maybe you could enlighten me a little?
controllers/testing/test_handler.go
Outdated
@@ -31,8 +33,8 @@ type UserTesting struct { | |||
|
|||
func (cobs *Cobs) Login(c *gin.Context) { | |||
var userCobs UserTesting | |||
if err := c.BindJSON(&userCobs); err != nil { | |||
cobs.Res.CustomResponse(c, "Content-Type", "application/json", "error", "failed when parsing data", http.StatusBadRequest, nil) | |||
if err := cobs.Binding.BindingJSON(c, &userCobs); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use ShouldBindJSON()
?
utils/bind/bind.go
Outdated
func (b *Binding) BindingJSON(c *gin.Context, obj interface{}) error { | ||
if err := c.ShouldBindWith(obj, binding.JSON); err != nil { | ||
if err := c.AbortWithError(http.StatusBadRequest, err).SetType(gin.ErrorTypeBind); err != nil { | ||
return err | ||
} | ||
return err | ||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really necessary? I mean gin already provided ShouldBindJSON()
, which exactly works like this
As you can see on docs. docs : i'll wrap it out, simply like these :
|
Okay, but the problem is lies in So the point of using if err := cobs.Binding.BindingJSON(c, &userCobs); err != nil {
cobs.Res.CustomResponse(c, "Content-Type", "application/json", "error", "failed when binding data", http.StatusNotFound, nil)
return
} to this if err := c.ShouldBindJSON(&userCobs); err != nil {
cobs.Res.CustomResponse(c, "Content-Type", "application/json", "error", err.Error(), http.StatusNotFound, nil)
return
} Then you start your bad request (no body, username with int datatype, etc.) with your postman and see the differences. |
Thx, i need to learn more and will delete that later. Don't feel like bothered by me, ok? |
Yeah, chill man. It's just a discussion nothing wrong with it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's all good now. Thank you! @huf0813
Hi, i see a lot of insight from @Endrawan and @AFauzulh for better code. Thx guys, i hope there will be more discussions later