Skip to content

Commit

Permalink
[doc] updating docs based on #667 (#713)
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Pagano authored and vishr committed Nov 8, 2016
1 parent c0822c0 commit f869c9d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 38 deletions.
87 changes: 58 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,32 @@ e.DELETE("/users/:id", deleteUser)
### Path Parameters

```go
// e.GET("/users/:id", getUser)
func getUser(c echo.Context) error {
// User ID from path `users/:id`
id := c.Param("id")
// User ID from path `users/: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

`/show?team=x-men&member=wolverine`

```go
//e.GET("/show", show)
func show(c echo.Context) error {
// Get team and member from the query string
team := c.QueryParam("team")
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`

`POST` `/save`
Expand All @@ -111,55 +119,76 @@ email | joe@labstack.com


```go
// e.POST("/save", save)
func save(c echo.Context) error {
// Get name and email
name := c.FormValue("name")
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`

`POST` `/save`

name | value
:--- | :---
name | Joe Smith
email | joe@labstack.com
avatar | avatar

```go
func save(c echo.Context) error {
// Get name and email
// Get name
name := c.FormValue("name")
email := c.FormValue("email")
// Get avatar
avatar, err := c.FormFile("avatar")
if err != nil {
return err
}

// Source
src, err := avatar.Open()
if err != nil {
return err
}
defer src.Close()

// Destination
dst, err := os.Create(avatar.Filename)
if err != nil {
return err
}
defer dst.Close()
avatar, err := c.FormFile("avatar")
if err != nil {
return err
}

// Source
src, err := avatar.Open()
if err != nil {
return err
}
defer src.Close()

// Destination
dst, err := os.Create(avatar.Filename)
if err != nil {
return err
}
defer dst.Close()

// Copy
if _, err = io.Copy(dst, src); err != nil {
return err
}

return c.HTML(http.StatusOK, "<b>Thank you! " + name + "</b>")
}
```

// Copy
if _, err = io.Copy(dst, src); err != nil {
return err
}
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.

return c.HTML(http.StatusOK, "<b>Thank you!</b>")
}
```sh
cd <project directory>
ls avatar.png
// => avatar.png
```

### Handling Request
Expand Down
23 changes: 14 additions & 9 deletions website/content/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ package main

import (
"net/http"

"github.com/labstack/echo"
"github.com/labstack/echo/engine/standard"
)

func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})

if err := e.Start(":1323"); err != nil {
e.Logger.Fatal(err.Error())
}
Expand Down Expand Up @@ -85,24 +85,26 @@ e.DELETE("/users/:id", deleteUser)
### Path Parameters

```go
// e.GET("/users/:id", getUser)
func getUser(c echo.Context) error {
// User ID from path `users/:id`
id := c.Param("id")
// User ID from path `users/: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

`/show?team=x-men&member=wolverine`

```go
//e.GET("/show", show)
func show(c echo.Context) error {
// Get team and member from the query string
team := c.QueryParam("team")
member := c.QueryParam("member")

return c.String(http.StatusOK, "team:" + team + ", member:" + member)
}
```
Expand All @@ -118,17 +120,19 @@ name | value
name | Joe Smith
email | joe@labstack.com


```go
// e.POST("/save", save)
func save(c echo.Context) error {
// Get name and email
name := c.FormValue("name")
email := c.FormValue("email")

return c.String(http.StatusOK, "name:" + name + ", email:" + email)
}
```

Run the following command.
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
Expand All @@ -148,7 +152,7 @@ func save(c echo.Context) error {
// Get name
name := c.FormValue("name")
// Get avatar
avatar, err := c.FormFile("avatar")
avatar, err := c.FormFile("avatar")
if err != nil {
return err
}
Expand Down Expand Up @@ -181,8 +185,9 @@ Run the following command.
$ 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.

To check the uploaded image, run the following command.
```sh
cd <project directory>
ls avatar.png
Expand Down

0 comments on commit f869c9d

Please sign in to comment.