Skip to content

Commit f0674ea

Browse files
committed
Improve request binding documentation. See Echo pull request #1681 (labstack/echo#1681)
1 parent 9665f96 commit f0674ea

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

website/content/guide/request.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,47 @@ description = "Handling HTTP request in Echo"
88

99
## Bind Data
1010

11-
To bind request body into a Go type use `Context#Bind(i interface{})`.
11+
Echo provides following method to bind data from different sources (path params, query params, request body) to structure
12+
`Context#Bind(i interface{})` method.
1213
The default binder supports decoding application/json, application/xml and
1314
application/x-www-form-urlencoded data based on the Content-Type header.
1415

16+
Request data is binded to the struct in given order:
17+
18+
1. Path parameters
19+
2. Query parameters
20+
3. Request body
21+
22+
Notes:
23+
24+
* Each step can overwrite binded fields from the previous step. This means if your json request has query param
25+
`&name=query` and body is `{"name": "body"}` then the result will be `User{Name: "body"}`.
26+
* To avoid security flaws try to avoid passing binded structs directly to other methods if
27+
these structs contain fields that should not be bindable. It is advisable to have separate struct for binding and map it
28+
explicitly to your business struct. Consider what will happen if your binded struct has public
29+
field `IsAdmin bool` and request body would contain `{IsAdmin: true, Name: "hacker"}`.
30+
* When binding forms take note that Echo implementation uses standard library form parsing which parses form data
31+
from BOTH URL and BODY if content type is not MIMEMultipartForm. See documentation for [non-MIMEMultipartForm](https://golang.org/pkg/net/http/#Request.ParseForm)
32+
and [MIMEMultipartForm](https://golang.org/pkg/net/http/#Request.ParseMultipartForm)
33+
* To bind data only from request body use following code
34+
```go
35+
if err := (&DefaultBinder{}).BindBody(c, &payload); err != nil {
36+
return err
37+
}
38+
```
39+
* To bind data only from query parameters use following code
40+
```go
41+
if err := (&DefaultBinder{}).BindQueryParams(c, &payload); err != nil {
42+
return err
43+
}
44+
```
45+
* To bind data only from path parameters use following code
46+
```go
47+
if err := (&DefaultBinder{}).BindPathParams(c, &payload); err != nil {
48+
return err
49+
}
50+
```
51+
1552
Example below binds the request payload into `User` struct based on tags:
1653

1754
```go
@@ -29,6 +66,15 @@ func(c echo.Context) (err error) {
2966
if err = c.Bind(u); err != nil {
3067
return
3168
}
69+
// To avoid security flaws try to avoid passing binded structs directly to other methods
70+
// if these structs contain fields that should not be bindable.
71+
user := UserDTO{
72+
Name: u.Name,
73+
Email: u.Email,
74+
IsAdmin: false // because you could accidentally expose fields that should not be bind
75+
}
76+
executeSomeBusinessLogic(user)
77+
3278
return c.JSON(http.StatusOK, u)
3379
}
3480
```

0 commit comments

Comments
 (0)