Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions website/content/guide/binding.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ In the struct definitions each field can be tagged to restrict binding to specif

* `query` - source is request query parameters.
* `param` - source is route path parameter.
* `header` - source is header parameter.
* `form` - source is form. Values are taken from query and request body. Uses Go standard library form parsing.
* `json` - source is request body. Uses Go [json](https://golang.org/pkg/encoding/json/) package for unmarshalling.
* `xml` - source is request body. Uses Go [xml](https://golang.org/pkg/encoding/xml/) package for unmarshalling.

```go
type User struct {
ID string `param:"id" query:"id" form:"id" json:"id" xml:"id"`
ID string `param:"id" query:"id" header:"id" form:"id" json:"id" xml:"id"`
}
```

Expand All @@ -35,7 +36,7 @@ Request data is bound to the struct in given order:

Notes:

* For `query`, `param`, `form` **only** fields **with** tags are bound.
* For `query`, `param`, `header`, `form` **only** fields **with** tags are bound.
* For `json` and `xml` can bind to *public* fields without tags but this is by their standard library implementation.
* Each step can overwrite bound fields from the previous step. This means if your json request has query param
`&name=query` and body is `{"name": "body"}` then the result will be `User{Name: "body"}`.
Expand Down Expand Up @@ -64,6 +65,12 @@ Notes:
return err
}
```
* To bind data only from header parameters use following code
```go
if err := (&DefaultBinder{}).BindHeaders(c, &payload); err != nil {
return err
}
```

### Example

Expand All @@ -88,7 +95,7 @@ e.POST("/users", func(c echo.Context) (err error) {
user := UserDTO{
Name: u.Name,
Email: u.Email,
IsAdmin: false // because you could accidentally expose fields that should not be bind
IsAdmin: false // because you could accidentally expose fields that should not be bound
}
executeSomeBusinessLogic(user)

Expand Down