Skip to content

Binding

Parsing request data is a crucial part of a web application. In Echo this is called binding, and it can read from four parts of an HTTP request:

  • URL path parameters
  • URL query parameters
  • Headers
  • Request body

Define a struct with tags specifying the data source and key, then call c.Bind() with a pointer to it. Here the query parameter id binds to the ID field:

type User struct {
ID string `query:"id"`
}
// handler for /users?id=<userID>
var user User
if err := c.Bind(&user); err != nil {
return c.String(http.StatusBadRequest, "bad request")