Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 615
Jesse van Bekkum Avatar asked Feb 17 '14 08:02

Jesse van Bekkum


People also ask

How does Python handle space in URL?

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.

How do you put a space in a query string?

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.

What is URL encode in Python?

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.

Does Python requests URL encode?

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.


3 Answers

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"))
like image 137
Brett Beatty Avatar answered Oct 14 '22 07:10

Brett Beatty


try it.

import urllib
urllib.urlencode(params)

http://docs.python.org/2/library/urllib.html#urllib.urlencode

like image 24
sinceq Avatar answered Oct 14 '22 05:10

sinceq


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
like image 1
Robert Lu Avatar answered Oct 14 '22 05:10

Robert Lu