fiber icon indicating copy to clipboard operation
fiber copied to clipboard

🤗 [Question]: How to handle decode inner struct from form http request?

Open bbkgh opened this issue 1 year ago • 3 comments

Question Description

Hi. I have an application with the following structs:

type SearchRequest struct {
    Limit      int          `json:"limit" validate:"min=1,max=200"`
    Offset     int          `json:"offset" validate:"min=0"`
    Filters    SearchFilter `json:"filters" form:"filters"`
}
type SearchFilter struct {
    Shop      []string `json:"shop" form:"shop"`
}

I call it with the following cURL command:

curl -X POST "https://mysite/search/" \
-F "image=@./test_image.png;type=image/png" \
-F "limit=20" \
-F "offset=0" \
-F 'filters={"shop":[]}'

I think this should work, but it results in the following error:

failed to decode: schema: converter not found for api.SearchFilter This endpoint works in application/json format (without sending file) How can I fix this?

Code Snippet (optional)

func Search(db *database.Database, validator *validator.Validate) fiber.Handler {
	return func(c *fiber.Ctx) error {
		body := new(SearchRequest)
		if err := c.BodyParser(body); err != nil {
			println(err.Error())
			return c.Status(400).SendString("Problem in request content")
		}
       }
}

Checklist:

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

bbkgh avatar Aug 11 '24 05:08 bbkgh

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

welcome[bot] avatar Aug 11 '24 05:08 welcome[bot]

@bbkgh What's your full handler, I dont see your struct being used in the provided code.

gaby avatar Aug 13 '24 12:08 gaby

@bbkgh What's your full handler, I dont see your struct being used in the provided code.

it's used in body := new(SearchRequest) .... c.BodyParser(body)....

bbkgh avatar Aug 13 '24 13:08 bbkgh