Use %20 instead of + for space in python query parameters

python, python-requests

Solution

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"))

Problem

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?

Original source