Request

Retrieve Data

Form Data

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

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

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

  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).

  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.

  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

Binding Data

Also binding of request data to native Go structs and variables is supported. See Binding Data

Validate Data

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

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

  1. package main
  2. import (
  3. "net/http"
  4. "github.com/go-playground/validator"
  5. "github.com/labstack/echo/v4"
  6. "github.com/labstack/echo/v4/middleware"
  7. )
  8. type (
  9. User struct {
  10. Name string `json:"name" validate:"required"`
  11. Email string `json:"email" validate:"required,email"`
  12. }
  13. CustomValidator struct {
  14. validator *validator.Validate
  15. }
  16. )
  17. func (cv *CustomValidator) Validate(i interface{}) error {
  18. if err := cv.validator.Struct(i); err != nil {
  19. // Optionally, you could return the error to give each route more control over the status code
  20. return echo.NewHTTPError(http.StatusBadRequest, err.Error())
  21. }
  22. return nil
  23. }
  24. func main() {
  25. e := echo.New()
  26. e.Validator = &CustomValidator{validator: validator.New()}
  27. e.POST("/users", func(c echo.Context) (err error) {
  28. u := new(User)
  29. if err = c.Bind(u); err != nil {
  30. return echo.NewHTTPError(http.StatusBadRequest, err.Error())
  31. }
  32. if err = c.Validate(u); err != nil {
  33. return err
  34. }
  35. return c.JSON(http.StatusOK, u)
  36. })
  37. e.Logger.Fatal(e.Start(":1323"))
  38. }
  1. curl -X POST http://localhost:1323/users \
  2. -H 'Content-Type: application/json' \
  3. -d '{"name":"Joe","email":"joe@invalid-domain"}'
  4. {"message":"Key: 'User.Email' Error:Field validation for 'Email' failed on the 'email' tag"}