I have written the following python script, using python requests (http://requests.readthedocs.org/en/latest/):
import requests
payload = {'key1': 'value 1', 'key2': 'value 2'}
headers = {'Content-Type': 'application/json;charset=UTF-8'}
r = requests.get("http://example.com/service", params=payload, headers=headers,
auth=("admin", "password"))
If I look at the access log of the server, the incoming request is: /service?key1=value++1&key2=value+2
However, the server expects ...value%20%201&
...
I have read that using a + as a placeholder for a space is part of content type application/x-www-form-urlencoded, but clearly I have requested application/json.
Anybody know how to use %20 as a space in query parameters of pythons requests?
The proper way of encoding a space in the query string of a URL is the + sign. See Wikipedia and the HTML specification. As such urllib. quote_plus() should be used instead when encoding just one key or value, or use urllib.
Our recommendation is to avoid using spaces in URLs, and instead use hyphens to separate words. If you are unable to do this, make sure to encode whitespace using "+" or "%20" in the query-string, and using "%20" within the rest of the URL.
urlencode() function. This is a convenience function which takes a dictionary of key value pairs or a sequence of two-element tuples and uses the quote_plus() function to encode every value. The resulting string is a series of key=value pairs separated by & character.
In Python, we can URL encode a query string using the urlib. parse module, which further contains a function urlencode() for encoding the query string in URL.
To follow up on @WeaselFox's answer, they introduced a patch that accepts a quote_via
keyword argument to urllib.parse.urlencode
. Now you could do this:
import requests
import urllib
payload = {'key1': 'value 1', 'key2': 'value 2'}
headers = {'Content-Type': 'application/json;charset=UTF-8'}
params = urllib.parse.urlencode(payload, quote_via=urllib.parse.quote)
r = requests.get("http://example.com/service", params=params, headers=headers,
auth=("admin", "password"))
try it.
import urllib
urllib.urlencode(params)
http://docs.python.org/2/library/urllib.html#urllib.urlencode
I only find urllib.parse.quote , which can replace space to %20
.
But quote
could not convert a dict.
so, We must use quote
to transform dict in advance.
#for python3
from urllib.parse import quote
payload = {'key1': 'value 1', 'key2': 'value 2'}
newpayload = {}
for (k, v) in payload.items():
newpayload[quote(k)] = quote(v)
print(newpayload)
#print result: {'key1': 'value%20%201', 'key2': 'value%202'}
# Now, you can use it in requests
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With