-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Binding json array fails when there is any parm in url #1565
Comments
I have the same issue Introduction of a write-able params would mean we can remove them if we call bind, that could be a solution Edit: Creating new functions would also allow the old to remain the same, thus keeping backwards compatibility. I can create a PR if maintainers are interested |
My current workaround includes a custom binder
|
I have the same problem. The description is misleading, because it binds the param as well, not only the request body...
My workaround: reset the params to nil by calling Unfortunately it doesn't work with existing QueryParams Of course reading the c.Request().Body is always possible. |
- for JSON, XML and Multipart payloads - without and with extra (dummy) query parameters, which make the tests fail (see e.g. labstack#1565)
Do not throw an error when binding to a slice, if binding occurs on path- or query-parameters => data is very likely to be found in the body, which will be bound later Should fix labstack#1356, labstack#1448, labstack#1495 (second case), labstack#1565
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
- for JSON, XML and Multipart payloads - without and with extra (dummy) query parameters, which make the tests fail (see e.g. labstack#1565)
Do not throw an error when binding to a slice, if binding occurs on path- or query-parameters => data is very likely to be found in the body, which will be bound later Should fix labstack#1356, labstack#1448, labstack#1495 (second case), labstack#1565
This is not a good practice, closing issues like this, that are valid but "stale"? |
This is current limitation for Details of possible methods to use can be found in documentation: https://echo.labstack.com/guide/binding/ In case you want to bind json body to slice you should use data := make([]uint, 0)
if err := (&echo.DefaultBinder{}).BindBody(ctx, &data); err != nil {
return err
} or go plain standard library data := make([]uint, 0)
if err := json.NewDecoder(ctx.Request().Body).Decode(data); err != nil {
return err
} If you want to bind both then you need have two separate bind calls: for example accessing path param as e.POST("/:id", func(ctx echo.Context) error {
var data struct{
ID string
Data []uint
}
if err := (&echo.DefaultBinder{}).BindBody(ctx, &data.Data); err != nil {
return err
}
data.ID = ctx.Param("id")
return ctx.JSON(200, data)
}) or binding to struct e.POST("/:id", func(ctx echo.Context) error {
var data struct{
ID string `param:"id"`
Data []uint
}
if err := (&echo.DefaultBinder{}).BindBody(ctx, &data.Data); err != nil {
return err
}
if err := (&echo.DefaultBinder{}).BindPathParams(ctx, &data); err != nil {
return err
}
return ctx.JSON(200, data)
}) or fluent api: e.POST("/:id", func(ctx echo.Context) error {
var data struct {
ID string
Data []uint
}
if err := json.NewDecoder(ctx.Request().Body).Decode(&data.Data); err != nil {
return err
}
if err := echo.PathParamsBinder(ctx).String("id", &data.ID).BindError(); err != nil {
return err
}
return ctx.JSON(200, data)
}) |
… not being struct. For path/query params binding we do not try (silently return) to bind when target is not struct. Recreates PR labstack#1574 and fixes labstack#1565
Issue Description
I want to bind a json array in the handler function but I can not do it when there is parm in url.
What is the right way to bind json array in the handler function?
Checklist
Expected behaviour
Actual behaviour
Steps to reproduce
First build and run the code.
Then use curl to create http requests.
It will returan the arrya in first url but not in second url.
Working code to debug
Version/commit
The text was updated successfully, but these errors were encountered: