2016-10-20 20:30:53 +02:00
|
|
|
+++
|
2016-11-17 08:46:00 +02:00
|
|
|
title = "Request"
|
2016-10-20 20:30:53 +02:00
|
|
|
description = "Handling HTTP request in Echo"
|
2016-11-21 00:16:22 +02:00
|
|
|
[menu.main]
|
2016-10-20 20:30:53 +02:00
|
|
|
name = "Request"
|
|
|
|
parent = "guide"
|
|
|
|
weight = 6
|
|
|
|
+++
|
|
|
|
|
2016-12-13 03:19:41 +02:00
|
|
|
## Bind Data
|
2016-10-20 20:30:53 +02:00
|
|
|
|
2016-12-13 03:19:41 +02:00
|
|
|
To bind request body into a Go type use `Context#Bind(i interface{})`.
|
2016-10-20 20:30:53 +02:00
|
|
|
The default binder supports decoding application/json, application/xml and
|
2016-12-13 03:19:41 +02:00
|
|
|
application/x-www-form-urlencoded data based on the Context-Type header.
|
|
|
|
|
|
|
|
Example below binds the request payload into `User` struct based on tags:
|
|
|
|
|
|
|
|
```go
|
|
|
|
// User
|
|
|
|
User struct {
|
|
|
|
Name string `json:"name" form:"name" query:"name"`
|
|
|
|
Email string `json:"email" form:"email" query:"email"`
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```go
|
|
|
|
// Handler
|
|
|
|
func(c echo.Context) (err error) {
|
|
|
|
u := new(User)
|
|
|
|
if err = c.Bind(u); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, u)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### JSON Data
|
|
|
|
|
|
|
|
```sh
|
|
|
|
curl \
|
|
|
|
-X POST \
|
|
|
|
http://localhost:1323/users \
|
|
|
|
-H 'Content-Type: application/json' \
|
|
|
|
-d '{"name":"Joe","email":"joe@labstack"}'
|
|
|
|
```
|
|
|
|
|
|
|
|
### Form Data
|
|
|
|
|
|
|
|
```sh
|
|
|
|
curl \
|
|
|
|
-X POST \
|
|
|
|
http://localhost:1323/users \
|
|
|
|
-d 'name=Joe' \
|
|
|
|
-d 'email=joe@labstack.com'
|
|
|
|
```
|
|
|
|
|
|
|
|
### Query Parameters
|
|
|
|
|
|
|
|
```sh
|
|
|
|
curl \
|
|
|
|
-X GET \
|
|
|
|
http://localhost:1323/users\?name\=Joe\&email\=joe@labstack.com
|
|
|
|
```
|
|
|
|
|
|
|
|
## Custom Binder
|
|
|
|
|
|
|
|
Custom binder can be registered using `Echo#Binder`.
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
*Example*
|
|
|
|
|
2016-12-13 03:19:41 +02:00
|
|
|
```go
|
|
|
|
type CustomBinder struct {}
|
|
|
|
|
|
|
|
func (cb *CustomBinder) Bind(i interface{}, c echo.Context) (err error) {
|
|
|
|
// You may use default binder
|
|
|
|
db := new(echo.DefaultBinder)
|
|
|
|
if err = db.Bind(i, c); err != echo.ErrUnsupportedMediaType {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define your custom implementation
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
```
|
2016-10-20 20:30:53 +02:00
|
|
|
|
2016-12-13 03:19:41 +02:00
|
|
|
## Retrieve Data
|
2016-10-20 20:30:53 +02:00
|
|
|
|
2016-12-13 03:19:41 +02:00
|
|
|
### Form Data
|
2016-10-20 20:30:53 +02:00
|
|
|
|
2016-12-13 03:19:41 +02:00
|
|
|
Form data can be retrieved by name using `Context#FormValue(name string)`.
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
*Example*
|
|
|
|
|
|
|
|
```go
|
2016-12-13 03:19:41 +02:00
|
|
|
// Handler
|
|
|
|
func(c echo.Context) error {
|
|
|
|
name := c.FormValue("name")
|
2016-10-20 20:30:53 +02:00
|
|
|
return c.String(http.StatusOK, name)
|
2016-12-13 03:19:41 +02:00
|
|
|
}
|
2016-10-20 20:30:53 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
```sh
|
2016-12-13 03:19:41 +02:00
|
|
|
curl \
|
|
|
|
-X POST \
|
|
|
|
http://localhost:1323 \
|
|
|
|
-d 'name=Joe'
|
2016-10-20 20:30:53 +02:00
|
|
|
```
|
|
|
|
|
2016-12-15 21:08:56 +02:00
|
|
|
To bind a custom data type, you can implement `Echo#BindUnmarshaler` interface.
|
|
|
|
|
|
|
|
*Example*
|
|
|
|
|
|
|
|
```go
|
|
|
|
type Timestamp time.Time
|
|
|
|
|
|
|
|
func (t *Timestamp) UnmarshalParam(src string) error {
|
|
|
|
ts, err := time.Parse(time.RFC3339, src)
|
|
|
|
*t = Timestamp(ts)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-12-13 03:19:41 +02:00
|
|
|
### Query Parameters
|
2016-10-20 20:30:53 +02:00
|
|
|
|
2016-12-13 03:19:41 +02:00
|
|
|
Query parameters can be retrieved by name using `Context#QueryParam(name string)`.
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
*Example*
|
|
|
|
|
|
|
|
```go
|
2016-12-13 03:19:41 +02:00
|
|
|
// Handler
|
|
|
|
func(c echo.Context) error {
|
|
|
|
name := c.QueryParam("name")
|
2016-10-20 20:30:53 +02:00
|
|
|
return c.String(http.StatusOK, name)
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
```sh
|
2016-12-13 03:19:41 +02:00
|
|
|
curl \
|
|
|
|
-X GET \
|
|
|
|
http://localhost:1323\?name\=Joe
|
2016-10-20 20:30:53 +02:00
|
|
|
```
|
|
|
|
|
2016-12-15 21:08:56 +02:00
|
|
|
Similar to form data, custom data type can be bind using `Context#QueryParam(name string)`.
|
|
|
|
|
2016-12-13 03:19:41 +02:00
|
|
|
### Path Parameters
|
2016-10-20 20:30:53 +02:00
|
|
|
|
2016-12-13 03:19:41 +02:00
|
|
|
Registered path parameters can be retrieved by name using `Context#Param(name string) string`.
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
*Example*
|
|
|
|
|
|
|
|
```go
|
|
|
|
e.GET("/users/:name", func(c echo.Context) error {
|
|
|
|
name := c.Param("name")
|
|
|
|
return c.String(http.StatusOK, name)
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
```sh
|
2016-12-13 03:19:41 +02:00
|
|
|
$ curl http://localhost:1323/users/Joe
|
2016-10-20 20:30:53 +02:00
|
|
|
```
|
|
|
|
|
2016-12-13 03:19:41 +02:00
|
|
|
## Validate Data
|
2016-10-20 20:30:53 +02:00
|
|
|
|
2016-12-16 04:44:06 +02:00
|
|
|
Echo doesn't have a built-in data validation capabilities, however, you can register
|
|
|
|
a custom validator using `Echo#Validator` and leverage third-party [libraries](https://github.com/avelino/awesome-go#validation).
|
2016-10-20 20:30:53 +02:00
|
|
|
|
2016-12-13 03:19:41 +02:00
|
|
|
Example below uses https://github.com/go-playground/validator framework for validation:
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
```go
|
2016-12-13 03:19:41 +02:00
|
|
|
type (
|
|
|
|
User struct {
|
|
|
|
Name string `json:"name" validate:"required"`
|
|
|
|
Email string `json:"email" validate:"required,email"`
|
2016-10-26 01:56:12 +02:00
|
|
|
}
|
|
|
|
|
2016-12-13 03:19:41 +02:00
|
|
|
CustomValidator struct {
|
|
|
|
validator *validator.Validate
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (cv *CustomValidator) Validate(i interface{}) error {
|
|
|
|
return cv.validator.Struct(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
e := echo.New()
|
|
|
|
e.Validator = &CustomValidator{validator: validator.New()}
|
|
|
|
e.POST("/users", func(c echo.Context) (err error) {
|
|
|
|
u := new(User)
|
|
|
|
if err = c.Bind(u); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = c.Validate(u); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, u)
|
|
|
|
})
|
|
|
|
e.Logger.Fatal(e.Start(":1323"))
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```sh
|
|
|
|
curl \
|
|
|
|
-X POST \
|
|
|
|
http://localhost:1323/users \
|
|
|
|
-H 'Content-Type: application/json' \
|
|
|
|
-d '{"name":"Joe","email":"joe@invalid-domain"}'
|
|
|
|
{"message":"Key: 'User.Email' Error:Field validation for 'Email' failed on the 'email' tag"}
|
2016-10-20 20:30:53 +02:00
|
|
|
```
|