-
Notifications
You must be signed in to change notification settings - Fork 8k
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
How to bind to a struct with json and header struct tags both ? #2309
Comments
type Request struct {
AppCode string `header:"appCode" json:"-" `
SomeID string `header:"-" json:"someId"`
UserID string `header:"userId" json:"-"`
EmailID string `header:"-" json:"emailId"`
} just use "-" to remove tag that you don't want. |
and |
what if I would like to use validation? |
I guess you could create an extra function like: type Request struct {
AppCode string `header:"appCode" json:"-" `
SomeID *string `header:"-" json:"someId"`
UserID string `header:"userId" json:"-"`
EmailID string `header:"-" json:"emailId"`
}
func (r *Request) Validate(context *gin.Context) bool {
if r.SomeID == nil {
// do something with context
context.abort()
return false
}
return true
} maibe it's not ideal, but i've been working this way for some specific stuff and all went good so far |
Hey!
and I always get
I tried without the required field but.. Doesn't help as I wished Thanks |
I think it's not possible to bind a struct which has both type RequestHeaders struct {
AppCode string `header:"appCode" binding:"required"`
UserId string `header:"userId"`
}
type RequestBody struct {
SomeId string `json:"someId" binding:"required"`
EmailId string `json:"emailId"`
}
func bindTest(ctx *gin.Context) {
var body RequestBody
var headers RequestHeaders
err = ctx.ShouldBindJSON(&body)
if err != nil {
err = errors.REQUEST_BIND_FAIL(err, request)
return
}
err = ctx.ShouldBindHeader(&headers)
if err != nil {
err = errors.REQUEST_BIND_FAIL(err, request)
return
}
} |
Nevermind that's kind of dumb just do this lol func (r *Router) AuthLogoutDevice(c *gin.Context) {
type Request struct {
UserID string `json:"-"`
AuthorizationToken string `json:"-"`
DeviceID string `json:"deviceID"`
}
// Bind the request
var request Request
var err error
request.UserID = c.GetHeader("user-id")
request.AuthorizationToken, err = getAuthorizationTokenFromHeaders(c)
if err != nil {
r.debugError(c, http.StatusInternalServerError, fmt.Errorf("failed to get authorization token from headers: %w", err))
return
}
if err := c.ShouldBindJSON(&request); err != nil {
r.debugError(c, http.StatusInternalServerError, fmt.Errorf("failed to bind JSON: %w", err))
return
}
// TODO
} |
Description
I have to place a request in a struct which uses fields from both headers and json body.
Is there a way to bind to a struct with json and header struct tags both ?
Like a ShouldBindWith which takes Json and header binding both ?
How to reproduce
Code :
Expectations
or
Actual result
Currently, Im getting a binding:required error from a field which only has a header tag and not json tag when I do ShouldBindJSON.
The text was updated successfully, but these errors were encountered: