-
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
feat: add methods c.BindCookie(obj)
from cookie and c.BindRequest(obj)
from http request
#2812
Conversation
…obj)` from http request
Hello, Not a gin maintainer, just a random passer-by here. First off, I love this API, I implemented a similar thing as a wrapper but it makes sense for gin to expose this directly. However, there is a small but fairly dangerous issue here: gin (and encoding/json) will try to guess the name of query fields on untagged struct fields, meaning if you have a struct like this: type MyRequest struct {
MyField string `query:"my_field"`
} ... all the binders will try to fill this field in the order they are called. Not just If you send this request:
... and bind with This also applies to fields that are not tagged at all, which can be dangerous security-wise. If you have, say, a There are ways around this:
Echo has a similar binding function and does 2 and 3. Just my 2 cents. Cheers! |
this is cause by using a same function another problem, gin has no by the way, it will be panic if body struct contains any of try this go mod edit -replace github.com/gin-gonic/gin=github.com/tangx/gin@v1.7.2-dev.requestbinding-alpha.1 example package main
import (
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.POST("/hello/:name", handler)
r.Run(":8881")
}
type MyRequest struct {
MyField string `query:"my_field"`
Name string `uri:"name"`
Data struct {
Name string `json:"name"`
} `body:""`
}
func handler(c *gin.Context) {
params := &MyRequest{}
err := c.ShouldBindRequest(params)
if err != nil {
c.JSON(200, err)
return
}
c.JSON(200, params)
} |
…o request body struct. (#1) * fix: bug uri, query, header, cookie can bind value into param body struct
That's what I want |
Why there is |
more info in https://github.com/tangx/gin#bind-request
|
try to use this to bind request |
master
Bind
method into 2 partsBindOnly()
/BindBodyOnly()
method, in this way, users can combine their own binding logic.Bind()
/BindBody()
method handle error fromBindOnly
/BindBodyOnly
, and do validation stuff.add `c.BindCookie(obj) method, which can bind values from request cookie into object.
add
c.BindRequest(obj)
method, which combinesuri, form , cookie, header, body
handlers. With this method, now can bind all wanted values at once.