Skip to content

Commit 9d4e793

Browse files
Fennygitbook-bot
authored andcommitted
GitBook: [master] one page modified
1 parent 9469fe9 commit 9d4e793

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

context.md

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ app.Post("/", func(c *fiber.Ctx) {
153153

154154
## BodyParser
155155

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:
157157

158158
* `application/json`
159159
* `application/xml`
@@ -168,34 +168,33 @@ c.BodyParser(out interface{}) error
168168

169169
{% code title="Example" %}
170170
```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-
182171
// Field names should start with an uppercase letter
183-
// Pass (o)
184-
// pass (x)
185172
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"`
188175
}
189176

190177
app.Post("/", func(c *fiber.Ctx) {
191-
var person Person
178+
p := new(Person)
192179

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+
}
196183

197-
// Do something with person.Name or person.Pass
184+
log.Println(p.Name) // john
185+
log.Println(p.Pass) // doe
198186
})
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"
199198
```
200199
{% endcode %}
201200

0 commit comments

Comments
 (0)