三味线
三味线
Published on 2020-06-06 / 62 Visits
0
0

python requests 请求传参数空格处理

import json
import urllib
import requests
param1 = {'name': 'hello world'}
param2 = urllib.parse.urlencode({'name': 'hello world'}, quote_via=urllib.parse.quote)
r = requests.get("http://111.111.111.11:1111/api/_/getConfig", params=param1)
if (r.status_code != 200):
  exit(1)
print(r.url)
print(r.json())

直接传参数,即使用param1,空格被替换为 '+'

http://111.111.111.11:1111/api/_/getConfig?name=hello+world

使用urllib转换,即使用param2,空格被替换为 '%20'

http://111.111.111.11:1111/api/_/getConfig?name=hello%20world

另外,如果直接使用{'name': 'hello%20world'},会被转换成:

http://111.111.111.11:1111/api/_/getConfig?name=hello%2520world

手动替换时不能使用Map,需使用字符串

param = 'name=hello%20world'

Requests官方文档: https://requests.readthedocs.io/en/latest/


Comment