-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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: Bind data using headers as source #1866
Conversation
Currently, echo supports binding data from query, path or body. Sometimes we need to read bind data from headers. It would be nice to automatically bind those using the `bindData` func, which is already well prepared to accept `http.Header`. I didn't add this to the `Bind` func, so this will not happen automatically. Main reason is backwards compatability. It might be confusing if variables are bound from headers when upgrading, and might even have become a security issue as pointed out in labstack#1670.
I realize we should add documentation over this seeing as this is a new feature and all. But I wanted to get the PR up here to see if this is something you'd be willing to accept. And if so, we can discuss next steps. |
Codecov Report
@@ Coverage Diff @@
## master #1866 +/- ##
==========================================
+ Coverage 89.57% 90.22% +0.64%
==========================================
Files 31 31
Lines 2735 2773 +38
==========================================
+ Hits 2450 2502 +52
+ Misses 184 173 -11
+ Partials 101 98 -3
Continue to review full report at Codecov.
|
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.
LGTM
Anything else you expect from me in this? |
Could you open a PR in https://github.com/labstack/echox/pulls to update the documentation please. |
func hello(c echo.Context) error {
c.Request().Header.Set("id", "1")
input := new(struct {
ID int `header:"id"`
})
if err := c.Bind(input); err != nil {
return err
}
fmt.Println(input)
return c.JSON(http.StatusOK, input)
} result: {"ID":0} ,c.Bind lacks support |
Yes, if err := (&echo.DefaultBinder{}).BindHeaders(c, input); err != nil {
return err
} This is intentional as original author points out #1866 (comment) |
Should we add this feature to Bind() in Echo v5 or next release? It should have some notice about breaking compatibility. |
Currently, echo supports binding data from query, path or body.
Sometimes we need to read bind data from headers. It would be nice to
automatically bind those using the
bindData
func, which is alreadywell prepared to accept
http.Header
.I didn't add this to the
Bind
func, so this will not happenautomatically. Main reason is backwards compatability. It might be
confusing if variables are bound from headers when upgrading, and might
even have become a security issue as pointed out in #1670.