@@ -57,7 +57,10 @@ func main() {
57
57
e.GET (" /" , func (c echo.Context ) error {
58
58
return c.String (http.StatusOK , " Hello, World!" )
59
59
})
60
- e.Run (standard.New (" :1323" ))
60
+
61
+ if err := e.Start (" :1323" ); err != nil {
62
+ e.Logger .Fatal (err.Error ())
63
+ }
61
64
}
62
65
```
63
66
@@ -85,8 +88,10 @@ e.DELETE("/users/:id", deleteUser)
85
88
func getUser (c echo .Context ) error {
86
89
// User ID from path `users/:id`
87
90
id := c.Param (" id" )
91
+ return c.String (http.StatusOK , id)
88
92
}
89
93
```
94
+ Browse to http://localhost:1323/users/Joe and you should see 'Joe' on the page.
90
95
91
96
### Query Parameters
92
97
@@ -97,9 +102,13 @@ func show(c echo.Context) error {
97
102
// Get team and member from the query string
98
103
team := c.QueryParam (" team" )
99
104
member := c.QueryParam (" member" )
105
+
106
+ return c.String (http.StatusOK , " team:" + team + " , member:" + member)
100
107
}
101
108
```
102
109
110
+ Browse to http://localhost:1323/show?team=x-men&member=wolverine and you should see 'team: x-men , member: wolverine ' on the page.
111
+
103
112
### Form ` application/x-www-form-urlencoded `
104
113
105
114
` POST ` ` /save `
@@ -114,51 +123,70 @@ func save(c echo.Context) error {
114
123
// Get name and email
115
124
name := c.FormValue (" name" )
116
125
email := c.FormValue (" email" )
126
+
127
+ return c.String (http.StatusOK , " name:" + name + " , email:" + email)
117
128
}
118
129
```
119
130
131
+ Run the following command.
132
+ ``` sh
133
+ $ curl -F " name=Joe Smith" -F " email=joe@labstack.com" http://localhost:1323/save
134
+ // => name:Joe Smith, email:joe@labstack.com
135
+ ```
136
+
120
137
### Form ` multipart/form-data `
121
138
122
139
` POST ` ` /save `
123
140
124
141
name | value
125
142
:--- | :---
126
143
name | Joe Smith
127
- email | joe@labstack.com
128
144
avatar | avatar
129
145
130
146
``` go
131
147
func save (c echo .Context ) error {
132
- // Get name and email
148
+ // Get name
133
149
name := c.FormValue (" name" )
134
- email := c.FormValue (" email" )
135
150
// Get avatar
136
151
avatar , err := c.FormFile (" avatar" )
137
- if err != nil {
138
- return err
139
- }
140
-
141
- // Source
142
- src , err := avatar.Open ()
143
- if err != nil {
144
- return err
145
- }
146
- defer src.Close ()
147
-
148
- // Destination
149
- dst , err := os.Create (avatar.Filename )
150
- if err != nil {
151
- return err
152
- }
153
- defer dst.Close ()
152
+ if err != nil {
153
+ return err
154
+ }
155
+
156
+ // Source
157
+ src , err := avatar.Open ()
158
+ if err != nil {
159
+ return err
160
+ }
161
+ defer src.Close ()
162
+
163
+ // Destination
164
+ dst , err := os.Create (avatar.Filename )
165
+ if err != nil {
166
+ return err
167
+ }
168
+ defer dst.Close ()
169
+
170
+ // Copy
171
+ if _, err = io.Copy (dst, src); err != nil {
172
+ return err
173
+ }
174
+
175
+ return c.HTML (http.StatusOK , " <b>Thank you! " + name + " </b>" )
176
+ }
177
+ ```
154
178
155
- // Copy
156
- if _, err = io.Copy (dst, src); err != nil {
157
- return err
158
- }
179
+ Run the following command.
180
+ ``` sh
181
+ $ curl -F " name=Joe Smith" -F " avatar=@/path/to/your/avatar.png" http://localhost:1323/save
182
+ // => < b> Thank you! Joe Smith< /b>
183
+ ```
159
184
160
- return c.HTML (http.StatusOK , " <b>Thank you!</b>" )
161
- }
185
+ To check the uploaded image, run the following command.
186
+ ``` sh
187
+ cd < project directory>
188
+ ls avatar.png
189
+ // => avatar.png
162
190
```
163
191
164
192
### Handling Request
0 commit comments