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"
|
|
|
|
[menu.side]
|
|
|
|
name = "Request"
|
|
|
|
parent = "guide"
|
|
|
|
weight = 6
|
|
|
|
+++
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
## Bind Request Body
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
To bind request body into a provided Go type use `Context#Bind(interface{})`.
|
|
|
|
The default binder supports decoding application/json, application/xml and
|
|
|
|
application/x-www-form-urlencoded payload based on Context-Type header.
|
|
|
|
|
|
|
|
*Example*
|
|
|
|
|
|
|
|
TODO
|
|
|
|
|
|
|
|
> Custom binder can be registered via `Echo#SetBinder(Binder)`
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
## Query Parameter
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
Query parameter can be retrieved by name using `Context#QueryParam(name string)`.
|
|
|
|
|
|
|
|
*Example*
|
|
|
|
|
|
|
|
```go
|
|
|
|
e.GET("/users", func(c echo.Context) error {
|
|
|
|
name := c.QueryParam("name")
|
|
|
|
return c.String(http.StatusOK, name)
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
```sh
|
|
|
|
$ curl -G -d "name=joe" http://localhost:1323/users
|
|
|
|
```
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
## Form Parameter
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
Form parameter can be retrieved by name using `Context#FormValue(name string)`.
|
|
|
|
|
|
|
|
*Example*
|
|
|
|
|
|
|
|
```go
|
|
|
|
e.POST("/users", func(c echo.Context) error {
|
|
|
|
name := c.FormValue("name")
|
|
|
|
return c.String(http.StatusOK, name)
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
```sh
|
|
|
|
$ curl -d "name=joe" http://localhost:1323/users
|
|
|
|
```
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
## Path Parameter
|
2016-10-20 20:30:53 +02:00
|
|
|
|
2016-10-26 01:56:12 +02:00
|
|
|
Registered path parameter can be retrieved by name `Context#Param(name string) string`.
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
*Example*
|
|
|
|
|
|
|
|
```go
|
|
|
|
e.GET("/users/:name", func(c echo.Context) error {
|
|
|
|
// By name
|
|
|
|
name := c.Param("name")
|
|
|
|
|
|
|
|
return c.String(http.StatusOK, name)
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
```sh
|
|
|
|
$ curl http://localhost:1323/users/joe
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
## Handler Path
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
`Context#Path()` returns the registered path for the handler, it can be used in the
|
|
|
|
middleware for logging purpose.
|
|
|
|
|
|
|
|
*Example*
|
|
|
|
|
|
|
|
```go
|
2016-10-26 01:56:12 +02:00
|
|
|
e.Use(func(handler echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
println(c.Path())
|
|
|
|
return handler(c)
|
|
|
|
}
|
2016-10-20 20:30:53 +02:00
|
|
|
})
|
2016-10-26 01:56:12 +02:00
|
|
|
|
2016-10-20 20:30:53 +02:00
|
|
|
e.GET("/users/:name", func(c echo.Context) error) {
|
|
|
|
return c.String(http.StatusOK, name)
|
|
|
|
})
|
|
|
|
```
|