echo icon indicating copy to clipboard operation
echo copied to clipboard

Unknow Type when binding slice of struct in formdata request

Open bashayerAlsalman opened this issue 4 years ago • 3 comments

Issue Description

can not bind a slice of struct when the request type is formdata

Checklist

  • [x] Dependencies installed
  • [x] No typos
  • [x] Searched existing issues and docs

Expected behaviour

Bind the formdata into the struct

Actual behaviour

Dose not bind formdata request and returns unknown type //err := c.Bind(s)

Steps to reproduce

when sending request

Working code to debug

type request struct {
     id                uint          `form:"id" json:"id"`
     Products   []product `form:"products"`
 }

 type product struct {
     Name          string      `form:"name"`
     Quantity      int           `form:"quantity"`
    Price              float32  `form:"price"`
}

Version/commit

bashayerAlsalman avatar Nov 08 '20 11:11 bashayerAlsalman

Could you please provide a full working example? Please include the code of the server and some request that shows your issue.

pafuent avatar Nov 10 '20 03:11 pafuent

Could you please provide a full working example? Please include the code of the server and some request that shows your issue.

Thank you for your response, please find this repo contains a running example https://github.com/bashayerAlsalman/formdata-with-struct-slice/tree/main

You may use this curl request to reproduce the issue

--header 'Accept-Encoding: multipart/form-data' \
--header 'Accept-Language: en' \
--header 'Cookie: JSESSIONID=05A66C75245B02ABE29D6B4C3715DCD9' \
--form 'id=123' \
--form 'products=[{"name":"tea", "price":12, "quantity":44}]' \
--form 'file=@/Users/imgs.png'```

bashayerAlsalman avatar Nov 10 '20 08:11 bashayerAlsalman

Thanks for the example. It help me to understand your needs. Sadly the current implementation of the Echo DefaultBinder doesn't support your use case. Also, you can check #1644

pafuent avatar Nov 11 '20 03:11 pafuent

The binder has seen some improvments to allow manual binding using the fluent binder or add a customer binder implementation.

See the docs for:

  • https://echo.labstack.com/guide/binding/#fluent-binding
  • https://echo.labstack.com/guide/binding/#custom-binding

lammel avatar Dec 02 '22 22:12 lammel