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/zh_CN/latest/
文章评论