Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃 [Question]: How to work with multipart.FileHeader and validation body parser? #2636

Closed
3 tasks done
hbinduni opened this issue Sep 17, 2023 · 2 comments
Closed
3 tasks done

Comments

@hbinduni
Copy link

Question Description

type MyForm struct {
	File     *multipart.FileHeader `form:"file" validate:"required"`
}

app.Post("/upload-v1", func(c *fiber.Ctx) error {
		// Parse the request body into MyForm
		form := new(MyForm)
		if err := c.BodyParser(form); err != nil {
			msg := fmt.Sprintf("Failed to parse the request body: %s", err.Error())
			return c.Status(http.StatusBadRequest).JSON(&fiber.Map{"ok": false, "msg": msg})
		}

		log.Printf("form: %+v", form) // <= this return form.File null

		return c.Status(http.StatusOK).JSON(&fiber.Map{"ok": true})
	})

i call the upload with this curl:

curl -X POST -F "file=@/tmp/data/test-data.txt" \
  http://localhost:3000/upload-v1

it give me form: &{File:<nil>} on the console.
and return error Validation failed: Key: 'MyForm.File' Error:Field validation for 'File' failed on the 'required' tag
what is the proper way to work wtih form data validation and body parser? does it support form data validation?

Code Snippet (optional)

No response

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.
@welcome
Copy link

welcome bot commented Sep 17, 2023

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

@efectn
Copy link
Member

efectn commented Sep 27, 2023

It will be implemented in v3 -> #2002. https://github.com/gorilla/schema doesn't support -*multipart.FileHeader

@efectn efectn closed this as completed Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants