Use instead of + for space in python query parameters

前端 未结 7 1277
猫巷女王i
猫巷女王i 2021-02-03 23:30

I have written the following python script, using python requests (http://requests.readthedocs.org/en/latest/):

import requests

payload = {\'key1\': \'value  1\         


        
7条回答
  •  一生所求
    2021-02-04 00:06

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

提交回复
热议问题