Python requests option converts params to "&"

Viewed 210

I'm trying to set params on a requests.get() the url is setting the & to & and the site appears to be rejecting the request.

Here is my sample code:

payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get('https://httpbin.org/get', params=payload)
print(r.url)
https://httpbin.org/get?key1=value1&key2=value2

expecting:

https://httpbin.org/get?key1=value1&key2=value2
1 Answers

My setup and version of the Requests library does not do what you are saying that your setup is doing. What I get as output when I run your code is:

https://httpbin.org/get?key1=value1&key2=value2

What's particularly interesting is that this sample code comes straight from the library's own documentation:

https://requests.readthedocs.io/en/master/user/quickstart/#passing-parameters-in-urls

And right there, they show what you should get from this code:

>>> print(r.url)
https://httpbin.org/get?key2=value2&key1=value1

It's a bit strange that in their version, the order of the two parameters is reversed, but since a map implies no ordering of its contents, I think that's ok. This is otherwise the same thing that I get...no encoded ampersands.

So why you are getting a result with the ampersands encoded, I can't say, but it seems that somehow, your configuration is other than the usual in that you're getting other than the usual result.

Related