Use instead of + for space in python query parameters

前端 未结 7 1282
猫巷女王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-03 23:52

    PYTHON 2.7

    • Override the urllib.quote_pluse with urllib.quote

    • The urlencoder uses urllib.quote_pluse to encode the data.


    code

    import requests
    import urllib
    urllib.quote_plus=urllib.quote # A fix for urlencoder to give %20 
    payload = {'key1': 'value  1', 'key2': 'value 2'}
    headers = {'Content-Type': 'application/json;charset=UTF-8'}
    param = urllib.urlencode(payload) #encodes the data
    r = requests.get("http://example.com/service", params=param, headers=headers, 
                 auth=("admin", "password"))
    

    output

    the output for param = urllib.urlencode(payload)
    'key2=value%202&key1=value%20%201' 
    

提交回复
热议问题