ggicci/httpin

How do you parse form data into nested struct?

iamucil opened this issue · 1 comments

I have form like this:

curl --location 'localhost/upload' \
--form 'body=@"/Users/Phantom/Documents/pdf/sample/sample.pdf"' \
--form 'title="document pdf new upload"' \
--form 'author.name="someone"' \
--form 'author.address="github"' \

and my struct is like this:

type Upload struct {
	Body   httpin.File `in:"form=body"`
	Title  string      `in:"form=title"`
	Author author      `in:"form=author"`
}

type author struct {
	Name    string `in:"form=name"`
	Address string `in:"form=address"`
}

I am unable to get value from author. How can I get author from form-data using nested struct?

ggicci commented

Hi, here's a same issue #59. httpin doesn't support nested structs. The input struct should be flat. You can use struct embedding instead. For example:

type Upload struct {
	Body   httpin.File `in:"form=body"`
	Title  string      `in:"form=title"`
	Author
}

type author struct {
	Name    string `in:"form=author.name"`
	Address string `in:"form=author.address"`
}