-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🐛 [Bug]: BodyParser does not parse multipart.FileHeader fields in multipart/form-data requests #3286
Comments
Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
https://docs.gofiber.io/api/ctx#bodyparser Pls test with the multipart form header |
package main
import (
"fmt"
"github.com/gofiber/fiber/v2"
"log"
"mime/multipart"
)
type Person struct {
Name string `json:"name" xml:"name" form:"name"`
Pass string `json:"pass" xml:"pass" form:"pass"`
File *multipart.FileHeader `json:"file" xml:"file" form:"file"`
}
func main() {
app := fiber.New()
app.Post("/", func(c *fiber.Ctx) error {
fmt.Println("header:", c.Get("Content-Type"))
p := new(Person)
if err := c.BodyParser(p); err != nil {
return err
}
//file, _ := c.FormFile("file"). // This place works
fmt.Printf("file:%v \n", p.File)
log.Println(p.Name) // "john"
log.Println(p.Pass) // "doe"
multi, err := c.MultipartForm()
if err != nil {
return err
}
fmt.Println("multi:", multi)
for key, values := range multi.Value {
for _, value := range values {
fmt.Println(key, value)
}
}
return c.Status(fiber.StatusOK).JSON(p)
//return c.SendStatus(fiber.StatusOK)
})
app.Listen(":3000")
} problem is that we only evaluate the value part of the multipart information and feed it into the struct should we also attach the file part to the struct or is there something against it? currently there are two maps |
In my opinion, attaching file fields to the struct would improve usability and consistency. It would allow developers to handle both textual fields and file uploads in a unified manner without additional manual steps like c.FormFile("file"). However, I see two considerations here:
app.Post("/", func(c *fiber.Ctx) error {
c.BodyParser(p, fiber.WithMultipartFileSupport())
}) This way, developers can opt-in for file parsing when needed. What do you think? |
related #2002 |
since we have a feature stop in version 2, we will no longer add this there in version 3 we are working on making this possible We will work on this in the issue #2002 |
@ReneWerner87. I believe we should aim to make Thoughts:
For v2, clarifying the current behavior in the docs is helpful, but for v3, I’d recommend prioritizing this improvement to meet developer expectations out of the box. |
Yeah |
Bug Description
When using the BodyParser method in Fiber, it fails to automatically parse file fields (multipart.FileHeader) in multipart/form-data requests. While other fields like name and pass are successfully parsed, file fields remain nil, even if they are present in the request payload. This makes it necessary to manually retrieve files using the c.FormFile method, which defeats the purpose of automatic parsing via BodyParser.
How to Reproduce
1-> Create a POST request using curl or Postman with the following payload:
Field name: john
Field pass: doe
File file: Upload any test file.
2-> Observe that the name and pass fields are parsed correctly, but the file field is not parsed (p.File is nil).
cURL
Outputs
Expected Behavior
The BodyParser method should correctly parse all fields in a multipart/form-data request, including files, into the corresponding struct fields (*multipart.FileHeader).
Fiber Version
v2.52.6
Code Snippet (optional)
Checklist:
The text was updated successfully, but these errors were encountered: