Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,7 @@ func (b *Bind) XML(out any) error {

// Form binds the form into the struct, map[string]string and map[string][]string.
// If Content-Type is "application/x-www-form-urlencoded" or "multipart/form-data", it will bind the form values.
//
// Binding multipart files is not supported yet.
// Multipart file fields are supported using *multipart.FileHeader, []*multipart.FileHeader, or *[]*multipart.FileHeader.
func (b *Bind) Form(out any) error {
bind := binder.GetFromThePool[*binder.FormBinding](&binder.FormBinderPool)
bind.EnableSplitting = b.ctx.App().config.EnableSplittingOnParsers
Expand Down
2 changes: 1 addition & 1 deletion docs/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ If not provided, TestConfig is set to the following defaults:

```go title="Default TestConfig"
config := fiber.TestConfig{
Timeout: time.Second(),
Timeout: time.Second,
FailOnTimeout: true,
}
```
Expand Down
118 changes: 65 additions & 53 deletions docs/api/bind.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ The binding sources have the following precedence:
4. **Request Headers**
5. **Cookies**

:::info
The request body is only included as a binding source when the request has both a non-empty body **and** a non-empty `Content-Type` header.
:::

```go title="Signature"
func (b *Bind) All(out any) error
```
Expand Down Expand Up @@ -94,14 +98,14 @@ type Person struct {

app.Post("/", func(c fiber.Ctx) error {
p := new(Person)

if err := c.Bind().Body(p); err != nil {
return err
}

log.Println(p.Name) // john
log.Println(p.Pass) // doe

// ...
})
```
Expand Down Expand Up @@ -146,14 +150,14 @@ type Person struct {

app.Post("/", func(c fiber.Ctx) error {
p := new(Person)

if err := c.Bind().CBOR(p); err != nil {
return err
}

log.Println(p.Name) // john
log.Println(p.Pass) // doe

// ...
})
```
Expand Down Expand Up @@ -182,14 +186,14 @@ type Person struct {

app.Post("/", func(c fiber.Ctx) error {
p := new(Person)

if err := c.Bind().Form(p); err != nil {
return err
}

log.Println(p.Name) // john
log.Println(p.Pass) // doe

// ...
})
```
Expand Down Expand Up @@ -217,15 +221,15 @@ type Person struct {

app.Post("/", func(c fiber.Ctx) error {
p := new(Person)

if err := c.Bind().Form(p); err != nil {
return err
}

log.Println(p.Name) // john
log.Println(p.Pass) // doe
log.Println(p.Avatar.Filename) // file.txt

// ...
})
```
Expand Down Expand Up @@ -254,14 +258,14 @@ type Person struct {

app.Post("/", func(c fiber.Ctx) error {
p := new(Person)

if err := c.Bind().JSON(p); err != nil {
return err
}

log.Println(p.Name) // john
log.Println(p.Pass) // doe

// ...
})
```
Expand Down Expand Up @@ -294,14 +298,14 @@ type Person struct {

app.Post("/", func(c fiber.Ctx) error {
p := new(Person)

if err := c.Bind().MsgPack(p); err != nil {
return err
}

log.Println(p.Name) // john
log.Println(p.Pass) // doe

// ...
})
```
Expand Down Expand Up @@ -331,14 +335,14 @@ type Person struct {

app.Post("/", func(c fiber.Ctx) error {
p := new(Person)

if err := c.Bind().XML(p); err != nil {
return err
}

log.Println(p.Name) // john
log.Println(p.Pass) // doe

// ...
})
```
Expand All @@ -351,7 +355,7 @@ curl -X POST -H "Content-Type: application/xml" --data "<login><name>john</name>

### Cookie

This method is similar to [Body Binding](#body), but for cookie parameters.
This method is similar to [Body Binding](#body), but for cookie parameters.
It is important to use the struct tag `cookie`. For example, if you want to parse a cookie with a field called `Age`, you would use a struct field with `cookie:"age"`.

```go title="Signature"
Expand All @@ -367,11 +371,11 @@ type Person struct {

app.Get("/", func(c fiber.Ctx) error {
p := new(Person)

if err := c.Bind().Cookie(p); err != nil {
return err
}

log.Println(p.Name) // Joseph
log.Println(p.Age) // 23
log.Println(p.Job) // true
Expand All @@ -386,7 +390,7 @@ curl --cookie "name=Joseph; age=23; job=true" http://localhost:8000/

### Header

This method is similar to [Body Binding](#body), but for request headers.
This method is similar to [Body Binding](#body), but for request headers.
It is important to use the struct tag `header`. For example, if you want to parse a request header with a field called `Pass`, you would use a struct field with `header:"pass"`.

```go title="Signature"
Expand All @@ -402,15 +406,15 @@ type Person struct {

app.Get("/", func(c fiber.Ctx) error {
p := new(Person)

if err := c.Bind().Header(p); err != nil {
return err
}

log.Println(p.Name) // john
log.Println(p.Pass) // doe
log.Println(p.Products) // [shoe hat]

// ...
})
```
Expand All @@ -423,7 +427,7 @@ curl "http://localhost:3000/" -H "name: john" -H "pass: doe" -H "products: shoe,

### Query

This method is similar to [Body Binding](#body), but for query parameters.
This method is similar to [Body Binding](#body), but for query parameters.
It is important to use the struct tag `query`. For example, if you want to parse a query parameter with a field called `Pass`, you would use a struct field with `query:"pass"`.

```go title="Signature"
Expand All @@ -439,18 +443,18 @@ type Person struct {

app.Get("/", func(c fiber.Ctx) error {
p := new(Person)

if err := c.Bind().Query(p); err != nil {
return err
}

log.Println(p.Name) // john
log.Println(p.Pass) // doe
// Depending on fiber.Config{EnableSplittingOnParsers: false} - default
log.Println(p.Products) // ["shoe,hat"]
// With fiber.Config{EnableSplittingOnParsers: true}
// log.Println(p.Products) // ["shoe", "hat"]

// ...
})
```
Expand Down Expand Up @@ -483,15 +487,15 @@ type Person struct {

app.Get("/", func(c fiber.Ctx) error {
p := new(Person)

if err := c.Bind().RespHeader(p); err != nil {
return err
}

log.Println(p.Name) // john
log.Println(p.Pass) // doe
log.Println(p.Products) // [shoe hat]

// ...
})
```
Expand All @@ -504,7 +508,7 @@ curl "http://localhost:3000/" -H "name: john" -H "pass: doe" -H "products: shoe,

### URI

This method is similar to [Body Binding](#body), but for path parameters.
This method is similar to [Body Binding](#body), but for path parameters.
It is important to use the struct tag `uri`. For example, if you want to parse a path parameter with a field called `Pass`, you would use a struct field with `uri:"pass"`.

```go title="Signature"
Expand All @@ -517,11 +521,11 @@ app.Get("/user/:id", func(c fiber.Ctx) error {
param := struct {
ID uint `uri:"id"`
}{}

if err := c.Bind().URI(&param); err != nil {
return err
}

// ...
return c.SendString(fmt.Sprintf("User ID: %d", param.ID))
})
Expand Down Expand Up @@ -574,7 +578,7 @@ app.Post("/custom", func(c fiber.Ctx) error {
})
```

Internally, custom binders are also used in the [Body](#body) method.
Internally, custom binders are also used in the [Body](#body) method.
The `MIMETypes` method is used to check if the custom binder should be used for the given content type.

## Options
Expand All @@ -583,7 +587,7 @@ For more control over error handling, you can use the following methods.

### WithAutoHandling

If you want to handle binder errors automatically, you can use `WithAutoHandling`.
If you want to handle binder errors automatically, you can use `WithAutoHandling`.
If there's an error, it will return the error and set HTTP status to `400 Bad Request`.
This function does NOT panic therefore you must still return on error explicitly

Expand All @@ -593,7 +597,7 @@ func (b *Bind) WithAutoHandling() *Bind

### WithoutAutoHandling

To handle binder errors manually, you can use the `WithoutAutoHandling` method.
To handle binder errors manually, you can use the `WithoutAutoHandling` method.
It's the default behavior of the binder.

```go title="Signature"
Expand All @@ -614,15 +618,23 @@ func (b *Bind) SkipValidation(skip bool) *Bind
Allows you to configure the BodyParser/QueryParser decoder based on schema options, providing the possibility to add custom types for parsing.

```go title="Signature"
func SetParserDecoder(parserConfig fiber.ParserConfig{
IgnoreUnknownKeys bool,
ParserType []fiber.ParserType{
Customtype any,
Converter func(string) reflect.Value,
},
ZeroEmpty bool,
SetAliasTag string,
})
func SetParserDecoder(parserConfig binder.ParserConfig)
```

`binder.ParserConfig` has the following fields:

```go
type ParserConfig struct {
IgnoreUnknownKeys bool
ParserType []ParserType
ZeroEmpty bool
SetAliasTag string
}

type ParserType struct {
CustomType any
Converter func(string) reflect.Value
}
```

```go title="Example"
Expand All @@ -644,15 +656,15 @@ var timeConverter = func(value string) reflect.Value {
return reflect.Value{}
}

customTime := fiber.ParserType{
customTime := binder.ParserType{
CustomType: CustomTime{},
Converter: timeConverter,
}

// Add custom type to the Decoder settings
fiber.SetParserDecoder(fiber.ParserConfig{
binder.SetParserDecoder(binder.ParserConfig{
IgnoreUnknownKeys: true,
ParserType: []fiber.ParserType{customTime},
ParserType: []binder.ParserType{customTime},
ZeroEmpty: true,
})

Expand Down Expand Up @@ -729,7 +741,7 @@ type Person struct {

app.Post("/", func(c fiber.Ctx) error {
p := new(Person)

if err := c.Bind().JSON(p); err != nil { // Receives validation errors
return err
}
Expand Down Expand Up @@ -765,7 +777,7 @@ app.Get("/", func(c fiber.Ctx) error {
log.Println(p.Name) // john
log.Println(p.Pass) // doe
log.Println(p.Products) // ["shoe", "hat"]

// ...
})
```
Expand Down
Loading
Loading