Request

Bind Data

To bind request body into a Go type use Context#Bind(i interface{}).The default binder supports decoding application/json, application/xml andapplication/x-www-form-urlencoded data based on the Content-Type header.

Example below binds the request payload into User struct based on tags:

  1. // User
  2. type User struct {
  3. Name string `json:"name" form:"name" query:"name"`
  4. Email string `json:"email" form:"email" query:"email"`
  5. }
  1. // Handler
  2. func(c echo.Context) (err error) {
  3. u := new(User)
  4. if err = c.Bind(u); err != nil {
  5. return
  6. }
  7. return c.JSON(http.StatusOK, u)
  8. }

JSON Data

  1. curl \
  2. -X POST \
  3. http://localhost:1323/users \
  4. -H 'Content-Type: application/json' \
  5. -d '{"name":"Joe","email":"[email protected]"}'

Form Data

  1. curl \
  2. -X POST \
  3. http://localhost:1323/users \
  4. -d 'name=Joe' \
  5. -d '[email protected]'

Query Parameters

  1. curl \
  2. -X GET \
  3. http://localhost:1323/users\?name\=Joe\&email\[email protected]

Custom Binder

Custom binder can be registered using Echo#Binder.

Example

  1. type CustomBinder struct {}
  2. func (cb *CustomBinder) Bind(i interface{}, c echo.Context) (err error) {
  3. // You may use default binder
  4. db := new(echo.DefaultBinder)
  5. if err = db.Bind(i, c); err != echo.ErrUnsupportedMediaType {
  6. return
  7. }
  8. // Define your custom implementation
  9. return
  10. }

Retrieve Data

Form Data

Form data can be retrieved by name using Context#FormValue(name string).

Example

  1. // Handler
  2. func(c echo.Context) error {
  3. name := c.FormValue("name")
  4. return c.String(http.StatusOK, name)
  5. }
  1. curl \
  2. -X POST \
  3. http://localhost:1323 \
  4. -d 'name=Joe'

To bind a custom data type, you can implement Echo#BindUnmarshaler interface.

Example

  1. type Timestamp time.Time
  2. func (t *Timestamp) UnmarshalParam(src string) error {
  3. ts, err := time.Parse(time.RFC3339, src)
  4. *t = Timestamp(ts)
  5. return err
  6. }

Query Parameters

Query parameters can be retrieved by name using Context#QueryParam(name string).

Example

  1. // Handler
  2. func(c echo.Context) error {
  3. name := c.QueryParam("name")
  4. return c.String(http.StatusOK, name)
  5. })
  1. curl \
  2. -X GET \
  3. http://localhost:1323\?name\=Joe

Similar to form data, custom data type can be bind using Context#QueryParam(name string).

Path Parameters

Registered path parameters can be retrieved by name using Context#Param(name string) string.

Example

  1. e.GET("/users/:name", func(c echo.Context) error {
  2. name := c.Param("name")
  3. return c.String(http.StatusOK, name)
  4. })
  1. $ curl http://localhost:1323/users/Joe

Validate Data

Echo doesn’t have built-in data validation capabilities, however, you can registera custom validator using Echo#Validator and leverage third-party libraries.

Example below uses https://github.com/go-playground/validator framework for validation:

  1. type (
  2. User struct {
  3. Name string `json:"name" validate:"required"`
  4. Email string `json:"email" validate:"required,email"`
  5. }
  6. CustomValidator struct {
  7. validator *validator.Validate
  8. }
  9. )
  10. func (cv *CustomValidator) Validate(i interface{}) error {
  11. return cv.validator.Struct(i)
  12. }
  13. func main() {
  14. e := echo.New()
  15. e.Validator = &CustomValidator{validator: validator.New()}
  16. e.POST("/users", func(c echo.Context) (err error) {
  17. u := new(User)
  18. if err = c.Bind(u); err != nil {
  19. return
  20. }
  21. if err = c.Validate(u); err != nil {
  22. return
  23. }
  24. return c.JSON(http.StatusOK, u)
  25. })
  26. e.Logger.Fatal(e.Start(":1323"))
  27. }
  1. curl \
  2. -X POST \
  3. http://localhost:1323/users \
  4. -H 'Content-Type: application/json' \
  5. -d '{"name":"Joe","email":"[email protected]"}'
  6. {"message":"Key: 'User.Email' Error:Field validation for 'Email' failed on the 'email' tag"}