-
-
Notifications
You must be signed in to change notification settings - Fork 168
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
Why does yarl encode query params differently? #301
Comments
Per a part in the aiohttp docs, at Passing Parameters in URLs:
But this seems to be contradicted by the section right below it!
And this is also contradicted empircally. If I pass the str
Then Additionally, it would be nice for |
I instead managed this problem passing the entire URL as a string to In my particular case, I have a query parameter with some
So I decided to use params = urllib.parse.urlencode(params, safe='')
encoded_url = f"{url}?{params}" And then: async with session.get(encoded_url, headers=signed_headers) as response: Thanks @bsolomon1124 for your analysis, I will watch this issue for an update |
Perhaps |
Curl is a bad example here. It doesn't really sanitize/encode things. If you feed it with garbage, it forwards that garbage in its requests. The others arguably target a different audience — end-users (their API is CLI) and are not libraries. For libraries, especially the low-level ones, it's more common to give more flexibility and responsibility to their users. So at the moment I don't know how I feel about this "max compat" idea. yarl does provide a way to feed it "raw"/pre-encoded data with the |
Seems like
|
Note: I'm not filing this issue to say "yarl is wrong, urllib is right"---I know that the author of yarl knows RFC 3986 way, way better than I ever will and it could be yarl that is doing the better job of adhering to it. But I'm curious what motivates the difference in behavior below.
Summary: We have an API endpoint where one of the query string params is
url=
.aiohttp
(viayarl.URL
) andrequests
(viaurllib.parse.urlencode
) encode the query string differently.Notice that the ? in the URL (within the query string) is not encoded:
With the
?
not encoded, the API endpoint gives us back a 400 response.Now with
urllib.parse
:Here the ? in the URL (within the query string) is encoded (as are the
/
).With the
?
encoded, the API endpoint gives us back a 200 response.So,
urlencode
has a default ofsafe=''
and encodes pretty much everything. Is there a reason thatyarl
seems to diverge from this?Now, when I look at RFC 3986, it actually seems like the
/
and?
should not need to be encoded:That is ironic, that it seems like yarl gets it "right" by leaving them unencoded, while urllib does too much work but then get us a 200 response.
So to make my question concrete:
aiohttp
, is there optionality to specify something likesafe
, or a callable that is called to the actual encoding ofparams
?The problem here is that even if
yarl
is "right," it basically forces the encoding even if I pass the full raw URL string to something likeaiohttp.request
:In
aiohttp
, the question then becomes "how can I override this behavior?" This is the answer I can come up with:Then pass
await fullencode(params)
torequest()
. (This assumes you have some base URL that forms_bp
above.)The text was updated successfully, but these errors were encountered: