1
0
mirror of https://github.com/labstack/echo.git synced 2025-07-03 00:56:59 +02:00

[doc] updating docs based on #667 (#713)

This commit is contained in:
Antonio Pagano
2016-11-08 13:19:30 -05:00
committed by Vishal Rana
parent c0822c0427
commit f869c9dbab
2 changed files with 72 additions and 38 deletions

View File

@ -82,24 +82,32 @@ e.DELETE("/users/:id", deleteUser)
### Path Parameters ### Path Parameters
```go ```go
// e.GET("/users/:id", getUser)
func getUser(c echo.Context) error { func getUser(c echo.Context) error {
// User ID from path `users/:id` // User ID from path `users/:id`
id := c.Param("id") id := c.Param("id")
return c.String(http.StatusOK, id)
} }
``` ```
Browse to http://localhost:1323/users/Joe and you should see 'Joe' on the page.
### Query Parameters ### Query Parameters
`/show?team=x-men&member=wolverine` `/show?team=x-men&member=wolverine`
```go ```go
//e.GET("/show", show)
func show(c echo.Context) error { func show(c echo.Context) error {
// Get team and member from the query string // Get team and member from the query string
team := c.QueryParam("team") team := c.QueryParam("team")
member := c.QueryParam("member") member := c.QueryParam("member")
return c.String(http.StatusOK, "team:" + team + ", member:" + member)
} }
``` ```
Browse to http://localhost:1323/show?team=x-men&member=wolverine and you should see 'team:x-men, member:wolverine' on the page.
### Form `application/x-www-form-urlencoded` ### Form `application/x-www-form-urlencoded`
`POST` `/save` `POST` `/save`
@ -111,13 +119,22 @@ email | joe@labstack.com
```go ```go
// e.POST("/save", save)
func save(c echo.Context) error { func save(c echo.Context) error {
// Get name and email // Get name and email
name := c.FormValue("name") name := c.FormValue("name")
email := c.FormValue("email") email := c.FormValue("email")
return c.String(http.StatusOK, "name:" + name + ", email:" + email)
} }
``` ```
Run the following command:
```sh
$ curl -F "name=Joe Smith" -F "email=joe@labstack.com" http://localhost:1323/save
// => name:Joe Smith, email:joe@labstack.com
```
### Form `multipart/form-data` ### Form `multipart/form-data`
`POST` `/save` `POST` `/save`
@ -125,14 +142,12 @@ func save(c echo.Context) error {
name | value name | value
:--- | :--- :--- | :---
name | Joe Smith name | Joe Smith
email | joe@labstack.com
avatar | avatar avatar | avatar
```go ```go
func save(c echo.Context) error { func save(c echo.Context) error {
// Get name and email // Get name
name := c.FormValue("name") name := c.FormValue("name")
email := c.FormValue("email")
// Get avatar // Get avatar
avatar, err := c.FormFile("avatar") avatar, err := c.FormFile("avatar")
if err != nil { if err != nil {
@ -158,10 +173,24 @@ func save(c echo.Context) error {
return err return err
} }
return c.HTML(http.StatusOK, "<b>Thank you!</b>") return c.HTML(http.StatusOK, "<b>Thank you! " + name + "</b>")
} }
``` ```
Run the following command.
```sh
$ curl -F "name=Joe Smith" -F "avatar=@/path/to/your/avatar.png" http://localhost:1323/save
// => <b>Thank you! Joe Smith</b>
```
For checking uploaded image, run the following command.
```sh
cd <project directory>
ls avatar.png
// => avatar.png
```
### Handling Request ### Handling Request
- Bind `JSON` or `XML` or `form` payload into Go struct based on `Content-Type` request header. - Bind `JSON` or `XML` or `form` payload into Go struct based on `Content-Type` request header.

View File

@ -48,8 +48,8 @@ package main
import ( import (
"net/http" "net/http"
"github.com/labstack/echo" "github.com/labstack/echo"
"github.com/labstack/echo/engine/standard"
) )
func main() { func main() {
@ -85,12 +85,14 @@ e.DELETE("/users/:id", deleteUser)
### Path Parameters ### Path Parameters
```go ```go
// e.GET("/users/:id", getUser)
func getUser(c echo.Context) error { func getUser(c echo.Context) error {
// User ID from path `users/:id` // User ID from path `users/:id`
id := c.Param("id") id := c.Param("id")
return c.String(http.StatusOK, id) return c.String(http.StatusOK, id)
} }
``` ```
Browse to http://localhost:1323/users/Joe and you should see 'Joe' on the page. Browse to http://localhost:1323/users/Joe and you should see 'Joe' on the page.
### Query Parameters ### Query Parameters
@ -98,11 +100,11 @@ Browse to http://localhost:1323/users/Joe and you should see 'Joe' on the page.
`/show?team=x-men&member=wolverine` `/show?team=x-men&member=wolverine`
```go ```go
//e.GET("/show", show)
func show(c echo.Context) error { func show(c echo.Context) error {
// Get team and member from the query string // Get team and member from the query string
team := c.QueryParam("team") team := c.QueryParam("team")
member := c.QueryParam("member") member := c.QueryParam("member")
return c.String(http.StatusOK, "team:" + team + ", member:" + member) return c.String(http.StatusOK, "team:" + team + ", member:" + member)
} }
``` ```
@ -118,17 +120,19 @@ name | value
name | Joe Smith name | Joe Smith
email | joe@labstack.com email | joe@labstack.com
```go ```go
// e.POST("/save", save)
func save(c echo.Context) error { func save(c echo.Context) error {
// Get name and email // Get name and email
name := c.FormValue("name") name := c.FormValue("name")
email := c.FormValue("email") email := c.FormValue("email")
return c.String(http.StatusOK, "name:" + name + ", email:" + email) return c.String(http.StatusOK, "name:" + name + ", email:" + email)
} }
``` ```
Run the following command. Run the following command:
```sh ```sh
$ curl -F "name=Joe Smith" -F "email=joe@labstack.com" http://localhost:1323/save $ curl -F "name=Joe Smith" -F "email=joe@labstack.com" http://localhost:1323/save
// => name:Joe Smith, email:joe@labstack.com // => name:Joe Smith, email:joe@labstack.com
@ -182,7 +186,8 @@ $ curl -F "name=Joe Smith" -F "avatar=@/path/to/your/avatar.png" http://localhos
// => <b>Thank you! Joe Smith</b> // => <b>Thank you! Joe Smith</b>
``` ```
To check the uploaded image, run the following command. For checking uploaded image, run the following command.
```sh ```sh
cd <project directory> cd <project directory>
ls avatar.png ls avatar.png