-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bind: add support for multipart file binding
- Loading branch information
Showing
5 changed files
with
115 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,6 +120,39 @@ curl -X POST -H "Content-Type: application/x-www-form-urlencoded" --data "name=j | |
curl -X POST -H "Content-Type: multipart/form-data" -F "name=john" -F "pass=doe" localhost:3000 | ||
``` | ||
|
||
:::info | ||
If you need to bind multipart file, you can use `*multipart.FileHeader`, `*[]*multipart.FileHeader` or `[]*multipart.FileHeader` as a field type. | ||
::: | ||
|
||
```go title="Example" | ||
type Person struct { | ||
Name string `form:"name"` | ||
Pass string `form:"pass"` | ||
Avatar *multipart.FileHeader `form:"avatar"` | ||
} | ||
|
||
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 | ||
|
||
// ... | ||
}) | ||
``` | ||
|
||
Run tests with the following `curl` command: | ||
|
||
```bash | ||
curl -X POST -H "Content-Type: multipart/form-data" -F "name=john" -F "pass=doe" -F 'avatar=@filename' localhost:3000 | ||
``` | ||
|
||
|
||
Check failure on line 155 in docs/api/bind.md
|
||
### JSON | ||
|
||
Binds the request JSON body to a struct. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters