@@ -153,7 +153,7 @@ app.Post("/", func(c *fiber.Ctx) {
153
153
154
154
## BodyParser
155
155
156
- Binds the request body to a struct. ` BodyParser ` supports decoding the following content types based on the ` Content-Type ` header:
156
+ Binds the request body to a struct. ` BodyParser ` supports decoding query parameters and the following content types based on the ` Content-Type ` header:
157
157
158
158
* ` application/json `
159
159
* ` application/xml `
@@ -168,34 +168,33 @@ c.BodyParser(out interface{}) error
168
168
169
169
{% code title="Example" %}
170
170
``` go
171
- // curl -X POST -H "Content-Type: application/json" \
172
- // --data '{"name":"john","pass":"doe"}' localhost:3000
173
-
174
- // curl -X POST -H "Content-Type: application/xml" \
175
- // --data '<Login><name>john</name><pass>doe</pass><Login>' localhost:3000
176
-
177
- // curl -X POST -H "Content-Type: application/x-www-form-urlencoded" \
178
- // --data 'name=john&pass=doe' localhost:3000
179
-
180
- // curl -v -F name=john -F pass=doe http://localhost:3000
181
-
182
171
// Field names should start with an uppercase letter
183
- // Pass (o)
184
- // pass (x)
185
172
type Person struct {
186
- Name string ` json:"name" xml:"name" form:"name" `
187
- Pass string ` json:"pass" xml:"pass" form:"pass" `
173
+ Name string ` json:"name" xml:"name"`
174
+ Pass string ` json:"pass" xml:"pass"`
188
175
}
189
176
190
177
app.Post (" /" , func (c *fiber.Ctx ) {
191
- var person Person
178
+ p := new ( Person)
192
179
193
- if err := c.BodyParser (&person ); err != nil {
194
- // Handle error
195
- }
180
+ if err := c.BodyParser (p ); err != nil {
181
+ log. Fatal (err)
182
+ }
196
183
197
- // Do something with person.Name or person.Pass
184
+ log.Println (p.Name ) // john
185
+ log.Println (p.Pass ) // doe
198
186
})
187
+ // Run tests with the following curl commands
188
+
189
+ // curl -X POST -H "Content-Type: application/json" --data "{\"name\":\"john\",\"pass\":\"doe\"}" localhost:3000
190
+
191
+ // curl -X POST -H "Content-Type: application/xml" --data "<login><name>john</name><pass>doe</pass></login>" localhost:3000
192
+
193
+ // curl -X POST -H "Content-Type: application/x-www-form-urlencoded" --data "name=john&pass=doe" localhost:3000
194
+
195
+ // curl -X POST -F name=john -F pass=doe http://localhost:3000
196
+
197
+ // curl -X POST "http://localhost:3000/?name=john&pass=doe"
199
198
```
200
199
{% endcode %}
201
200
0 commit comments