forked from labstack/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…, fixed labstack#305 Signed-off-by: Vishal Rana <[email protected]>
- Loading branch information
Showing
4 changed files
with
150 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,88 +7,188 @@ description = "Handling HTTP request in Echo" | |
weight = 6 | ||
+++ | ||
|
||
## Bind Request Body | ||
## Bind Data | ||
|
||
To bind request body into a provided Go type use `Context#Bind(interface{})`. | ||
To bind request body into a Go type use `Context#Bind(i interface{})`. | ||
The default binder supports decoding application/json, application/xml and | ||
application/x-www-form-urlencoded payload based on Context-Type header. | ||
application/x-www-form-urlencoded data based on the Context-Type header. | ||
|
||
Example below binds the request payload into `User` struct based on tags: | ||
|
||
```go | ||
// User | ||
User struct { | ||
Name string `json:"name" form:"name" query:"name"` | ||
Email string `json:"email" form:"email" query:"email"` | ||
} | ||
``` | ||
|
||
```go | ||
// Handler | ||
func(c echo.Context) (err error) { | ||
u := new(User) | ||
if err = c.Bind(u); err != nil { | ||
return | ||
} | ||
return c.JSON(http.StatusOK, u) | ||
} | ||
``` | ||
|
||
### JSON Data | ||
|
||
```sh | ||
curl \ | ||
-X POST \ | ||
http://localhost:1323/users \ | ||
-H 'Content-Type: application/json' \ | ||
-d '{"name":"Joe","email":"joe@labstack"}' | ||
``` | ||
|
||
### Form Data | ||
|
||
```sh | ||
curl \ | ||
-X POST \ | ||
http://localhost:1323/users \ | ||
-d 'name=Joe' \ | ||
-d '[email protected]' | ||
``` | ||
|
||
### Query Parameters | ||
|
||
```sh | ||
curl \ | ||
-X GET \ | ||
http://localhost:1323/users\?name\=Joe\&email\=[email protected] | ||
``` | ||
|
||
## Custom Binder | ||
|
||
Custom binder can be registered using `Echo#Binder`. | ||
|
||
*Example* | ||
|
||
TODO | ||
```go | ||
type CustomBinder struct {} | ||
|
||
func (cb *CustomBinder) Bind(i interface{}, c echo.Context) (err error) { | ||
// You may use default binder | ||
db := new(echo.DefaultBinder) | ||
if err = db.Bind(i, c); err != echo.ErrUnsupportedMediaType { | ||
return | ||
} | ||
|
||
// Define your custom implementation | ||
|
||
return | ||
} | ||
``` | ||
|
||
> Custom binder can be registered via `Echo#SetBinder(Binder)` | ||
## Retrieve Data | ||
|
||
## Query Parameter | ||
### Form Data | ||
|
||
Query parameter can be retrieved by name using `Context#QueryParam(name string)`. | ||
Form data can be retrieved by name using `Context#FormValue(name string)`. | ||
|
||
*Example* | ||
|
||
```go | ||
e.GET("/users", func(c echo.Context) error { | ||
name := c.QueryParam("name") | ||
// Handler | ||
func(c echo.Context) error { | ||
name := c.FormValue("name") | ||
return c.String(http.StatusOK, name) | ||
}) | ||
} | ||
``` | ||
|
||
```sh | ||
$ curl -G -d "name=joe" http://localhost:1323/users | ||
curl \ | ||
-X POST \ | ||
http://localhost:1323 \ | ||
-d 'name=Joe' | ||
``` | ||
|
||
## Form Parameter | ||
### Query Parameters | ||
|
||
Form parameter can be retrieved by name using `Context#FormValue(name string)`. | ||
Query parameters can be retrieved by name using `Context#QueryParam(name string)`. | ||
|
||
*Example* | ||
|
||
```go | ||
e.POST("/users", func(c echo.Context) error { | ||
name := c.FormValue("name") | ||
// Handler | ||
func(c echo.Context) error { | ||
name := c.QueryParam("name") | ||
return c.String(http.StatusOK, name) | ||
}) | ||
``` | ||
|
||
```sh | ||
$ curl -d "name=joe" http://localhost:1323/users | ||
curl \ | ||
-X GET \ | ||
http://localhost:1323\?name\=Joe | ||
``` | ||
|
||
## Path Parameter | ||
### Path Parameters | ||
|
||
Registered path parameter can be retrieved by name `Context#Param(name string) string`. | ||
Registered path parameters can be retrieved by name using `Context#Param(name string) string`. | ||
|
||
*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 | ||
$ curl http://localhost:1323/users/Joe | ||
``` | ||
|
||
## Validate Data | ||
|
||
## Handler Path | ||
Echo doesn't have built-in data validation capabilities, however you can set a | ||
custom validator using `Echo#Validator` and leverage third-party [libraries](https://github.com/avelino/awesome-go#validation). | ||
|
||
`Context#Path()` returns the registered path for the handler, it can be used in the | ||
middleware for logging purpose. | ||
|
||
*Example* | ||
Example below uses https://github.com/go-playground/validator framework for validation: | ||
|
||
```go | ||
e.Use(func(handler echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
println(c.Path()) | ||
return handler(c) | ||
type ( | ||
User struct { | ||
Name string `json:"name" validate:"required"` | ||
Email string `json:"email" validate:"required,email"` | ||
} | ||
}) | ||
|
||
e.GET("/users/:name", func(c echo.Context) error) { | ||
return c.String(http.StatusOK, name) | ||
}) | ||
CustomValidator struct { | ||
validator *validator.Validate | ||
} | ||
) | ||
|
||
func (cv *CustomValidator) Validate(i interface{}) error { | ||
return cv.validator.Struct(i) | ||
} | ||
|
||
func main() { | ||
e := echo.New() | ||
e.Validator = &CustomValidator{validator: validator.New()} | ||
e.POST("/users", func(c echo.Context) (err error) { | ||
u := new(User) | ||
if err = c.Bind(u); err != nil { | ||
return | ||
} | ||
if err = c.Validate(u); err != nil { | ||
return | ||
} | ||
return c.JSON(http.StatusOK, u) | ||
}) | ||
e.Logger.Fatal(e.Start(":1323")) | ||
} | ||
``` | ||
|
||
```sh | ||
curl \ | ||
-X POST \ | ||
http://localhost:1323/users \ | ||
-H 'Content-Type: application/json' \ | ||
-d '{"name":"Joe","email":"joe@invalid-domain"}' | ||
{"message":"Key: 'User.Email' Error:Field validation for 'Email' failed on the 'email' tag"} | ||
``` |