-
Notifications
You must be signed in to change notification settings - Fork 18k
net/url: url.Values.Encode uses =
for empty value in key=value
#30025
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
Comments
/cc @bradfitz |
@nathj07 What you have is valid. Its up to the server side to decide how to decode the query component. |
Ok I see. In our use case we use
So in the case of a URL such as "http://example.com/path/to/page?queryalue" thee result is "http://example.com/path/to/page?queryalue=". It seems that issue is in the |
=
on the query params when there is only a single key=
on the query params when there is only a single key
Related:
I supposed we could add another bool for this purpose. I forget what these URLs are called. They used to be generated from |
@bradfitz this was discovered within our web crawler. So the url belongs to another site and was originally generated there. Our crawler discovered it in its original form. The act of parsing and normalizing within our system results in the trailing '=' |
@bradfitz Values assumes you always have a value associated with every key. Values also allow you to have multiple keys, yet that would seem invalid for this case. Of course, this would be a breaking change unless there is something else you were thinking of. |
Yeah, that'd only work for the case where it's purely the old-style I think this should just be put on hold until the package/type is redesigned. At least the URL itself round-trips: https://play.golang.org/p/mlPt1b2Q6J7 ... It's only the url.Values map that doesn't. |
Looks like bug is from buf.WriteString(keyEscaped)
buf.WriteByte('=') // <<-- need add only in case non empty v
buf.WriteString(QueryEscape(v)) But the function is used for building POST form and GET query. |
=
on the query params when there is only a single key=
for empty value in key=value
The playground example is setting
If you don't want a trailing =, you can remove it before assigning to u.RawQuery. Part of the reason for having the RawQuery field at all is so that users have complete control over the exact spelling of the query string. The objection might be that params.Encode chooses to always use an = for empty values, but I don't believe that's something we would be wise to change at this point. It's easy to call a different function that behaves the way you want - package net/url is not forcing the use of this particular encoder. I don't think there's much we can do here (short of adding a second encoder entry point, which seems a bit overkill). |
What version of Go are you using (
go version
)?Also
Does this issue reproduce with the latest release?
I haven't tried the latest release directly I have tried on the playground though: https://play.golang.org/p/LSBliQAmXLc
What operating system and processor architecture are you using (
go env
)?go env
OutputAnd..
go env
OutputWhat did you do?
https://play.golang.org/p/LSBliQAmXLc
What did you expect to see?
I would expect the URL to not have the trailing
=
What did you see instead?
There is a trailing
=
This of course may be down to the owner of the site in question (anonymised in the playground) not having a fully valid URL - a query string key with no value. Though from my reading of https://tools.ietf.org/html/rfc3986#section-3.4 that seems valid if a little unconventional
The text was updated successfully, but these errors were encountered: