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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 tasks done
bbkgh opened this issue Aug 11, 2024 · 3 comments
Open
3 tasks done

Comments

@bbkgh
Copy link

bbkgh commented Aug 11, 2024

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:

  • 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.
Copy link

welcome bot commented Aug 11, 2024

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

@gaby
Copy link
Member

gaby commented Aug 13, 2024

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

@bbkgh
Copy link
Author

bbkgh commented Aug 13, 2024

@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)....

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