2015-10-02 03:24:38 +02:00
|
|
|
---
|
|
|
|
title: Request
|
|
|
|
menu:
|
2015-10-08 22:54:31 +02:00
|
|
|
side:
|
2015-10-02 03:24:38 +02:00
|
|
|
parent: guide
|
2015-10-06 00:52:07 +02:00
|
|
|
weight: 5
|
2015-10-02 03:24:38 +02:00
|
|
|
---
|
|
|
|
|
2015-11-13 06:23:14 +02:00
|
|
|
### Handler path
|
|
|
|
|
2015-11-22 20:38:02 +02:00
|
|
|
`Context#Path()` returns the registered path for a handler, it can be used in the
|
|
|
|
middleware for logging purpose.
|
2015-11-13 06:23:14 +02:00
|
|
|
|
|
|
|
*Example*
|
|
|
|
|
|
|
|
```go
|
|
|
|
e.Use(func(c *echo.Context) error {
|
2015-11-21 05:13:22 +02:00
|
|
|
println(c.Path()) // Prints `/users/:name`
|
2015-11-13 06:23:14 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
e.Get("/users/:name", func(c *echo.Context) error) {
|
|
|
|
return c.String(http.StatusOK, name)
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
2015-11-22 20:38:02 +02:00
|
|
|
### golang.org/x/net/context
|
|
|
|
|
|
|
|
`echo.Context` embeds `context.Context` interface, so all it's properties
|
|
|
|
are available right from `echo.Context`.
|
|
|
|
|
|
|
|
*Example*
|
|
|
|
|
|
|
|
```go
|
|
|
|
e.Get("/users/:name", func(c *echo.Context) error) {
|
|
|
|
c.Context = context.WithValue(nil, "key", "val")
|
|
|
|
// Pass it down...
|
|
|
|
// Use it...
|
|
|
|
println(c.Value("key"))
|
|
|
|
return c.String(http.StatusOK, name)
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
2015-10-02 03:24:38 +02:00
|
|
|
### Path parameter
|
|
|
|
|
2015-11-22 20:38:02 +02:00
|
|
|
Path parameter can be retrieved either by name `Context#Param(name string) string`
|
|
|
|
or by index `Context#P(i int) string`. Getting parameter by index gives a slightly
|
2015-10-02 03:24:38 +02:00
|
|
|
better performance.
|
|
|
|
|
|
|
|
*Example*
|
|
|
|
|
|
|
|
```go
|
|
|
|
e.Get("/users/:name", func(c *echo.Context) error {
|
|
|
|
// By name
|
|
|
|
name := c.Param("name")
|
|
|
|
|
|
|
|
// By index
|
|
|
|
name := c.P(0)
|
|
|
|
|
|
|
|
return c.String(http.StatusOK, name)
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
```sh
|
|
|
|
$ curl http://localhost:1323/users/joe
|
|
|
|
```
|
|
|
|
|
|
|
|
### Query parameter
|
|
|
|
|
2015-11-22 20:38:02 +02:00
|
|
|
Query parameter can be retrieved by name using `Context#Query(name string)`.
|
2015-10-02 03:24:38 +02:00
|
|
|
|
|
|
|
*Example*
|
|
|
|
|
|
|
|
```go
|
|
|
|
e.Get("/users", func(c *echo.Context) error {
|
|
|
|
name := c.Query("name")
|
|
|
|
return c.String(http.StatusOK, name)
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
```sh
|
|
|
|
$ curl -G -d "name=joe" http://localhost:1323/users
|
|
|
|
```
|
|
|
|
|
|
|
|
### Form parameter
|
|
|
|
|
2015-11-22 20:38:02 +02:00
|
|
|
Form parameter can be retrieved by name using `Context#Form(name string)`.
|
2015-10-02 03:24:38 +02:00
|
|
|
|
|
|
|
*Example*
|
|
|
|
|
|
|
|
```go
|
|
|
|
e.Post("/users", func(c *echo.Context) error {
|
|
|
|
name := c.Form("name")
|
|
|
|
return c.String(http.StatusOK, name)
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
```sh
|
|
|
|
$ curl -d "name=joe" http://localhost:1323/users
|
|
|
|
```
|