Skip to content

Request

A handler reads request data through the echo.Context. Echo can retrieve values individually by name, bind them into structs (see Binding), and hand off validation to a validator you register.

Retrieve a form field by name with Context#FormValue(name string):

e.POST("/form", func(c *echo.Context) error {
name := c.FormValue("name")
return c.String(http.StatusOK, name)
})

For types other than string, use the generic echo.FormValue[T] function:

age, err := echo.FormValue[int](c, "age")
if err != nil {
return err
}

Test with:

Terminal window
curl -X POST http://localhost:1323/form -d 'name=Joe&age=30'

To bind a custom data type, implement the echo.BindUnmarshaler interface:

type Timestamp time.Time
func (t *Timestamp) UnmarshalParam(src string) error {
ts, err := time.Parse(time.RFC3339, src)
if err != nil {
return err
}
*t = Timestamp(ts)
return nil
}

Retrieve a query parameter by name with Context#QueryParam(name string):

func(c *echo.Context) error {
name := c.QueryParam("name")
return c.String(http.StatusOK, name)
}

For types other than string, use the generic echo.QueryParam[T] function:

age, err := echo.QueryParam[int](c, "age")
if err != nil