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

Unable to bind query parameters after update #1932

Closed
aka-mj opened this issue Jul 22, 2021 · 4 comments
Closed

Unable to bind query parameters after update #1932

aka-mj opened this issue Jul 22, 2021 · 4 comments

Comments

@aka-mj
Copy link

aka-mj commented Jul 22, 2021

Issue Description

Unable to bind query parameters after update.

URL and query parameters:
POST 10.42.0.1:1232/api/v1/Settings/WirelessNetwork?ssid=TEST&password=pass123

Expected behaviour

After c.Bind(req) I'd expect req to hold the query parameters like it previously worked.

Actual behaviour

All fields in req are empty.

Working code to debug

type Request struct {
   SSID string `query:"ssid"`
   Pass string `query:"password"`
}

req := new(Request)
c.Bind(req)
if req.SSID == "" {
  fmt.Println("SSID empty")
}

Prints:

SSID empty

Version/commit

This broke after I updated from v4.1.17 -> v4.4.0

The fields used to be tagged json:"ssid" but looking through the docs this looks to have changed. I'm assuming I'm still missing something. Would hate to revert to the old release if I can't get this working.

@aka-mj
Copy link
Author

aka-mj commented Jul 22, 2021

I just saw the line

Query parameters (only for GET/DELETE methods)

Since I'm using a POST I guess this is no longer supported.

@aldas
Copy link
Contributor

aldas commented Jul 22, 2021

atm use as a workaround

		req := new(Request)
		if err := (&echo.DefaultBinder{}).BindQueryParams(c, req); err != nil {
			return err
		}

binding query params for POST methods was disabled because even public fields (without tags) were bound from query params. But with change "only explicit tags are bound" (v4.2.0 I think) we could allow it.

@aldas
Copy link
Contributor

aldas commented Jul 22, 2021

Note: if you used POST and c.Bind() then your req fields would/could have been overwritten by JSON payload as Echo uses default json decoder which will write to public fields even if those do not have json tag.

also there we many other problems with binding query params that way. see: #1670 #1638 #1356 #1565 #1448 #1495

@aka-mj
Copy link
Author

aka-mj commented Jul 22, 2021

Thanks @aldas, this worked. Moving forward it sounds like the recommended approach if using a POST is to put these fields in the body rather than as query parameters, is that correct?

@aka-mj aka-mj closed this as completed Jul 26, 2021
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