Python请求模块url编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30514459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python requests module url encode
提问by min2bro
I'm trying to send json as a parameter thru a get method for an api, I found that the url to which it is hitting is little bit different from the original url. Some ":%20" text is inserted in between the url. Not sure why this difference is coming, Can someone help
我正在尝试通过 api 的 get 方法将 json 作为参数发送,我发现它所访问的 url 与原始 url 略有不同。一些 ":%20" 文本被插入到 url 之间。不知道为什么会出现这种差异,有人可以帮忙吗
Original URL: http://258.198.39.215:8280/areas/0.1/get/raj/name?jsonRequest=%7B%22rajNames%22%3A%5B%22WAR%22%5D%7D
My URL : http://258.198.39.215:8280/areas/0.1/get/raj/name?jsonRequest=&%7B%22rajNames%22:%20%22WAR%22%7D
Python code:
蟒蛇代码:
headers = {'Accept': 'application/json','Authorization': 'Bearer '+access_token}
json = {'rajNames':'WAR'}
url = 'http://258.198.39.215:8280/areas/0.1/get/raj/name?jsonRequest='
r = requests.get(url, params=json.dumps(json),headers=headers)
print _r.url
采纳答案by Martijn Pieters
The spaces are not the problem; your method of generating the query string is, as is your actual JSON payload.
空间不是问题;您生成查询字符串的方法是,您的实际 JSON 负载也是如此。
Note that your original URL has a differentJSON structure:
请注意,您的原始 URL 具有不同的JSON 结构:
>>> from urllib import unquote
>>> unquote('%7B%22rajNames%22%3A%5B%22WAR%22%5D%7D')
'{"rajNames":["WAR"]}'
The rajNames
parameter is a list, not a single string.
该rajNames
参数是一个列表,而不是一个字符串。
Next, requests
sees all data in params
as a newparameter, so it used &
to delimit from the previous parameter. Use a dictionary and leave the ?jsonRequest=
part to requests
to generate:
接下来,requests
将所有数据params
视为新参数,因此它用于&
与前一个参数分隔。使用字典并将?jsonRequest=
部分留给requests
生成:
headers = {'Accept': 'application/json', 'Authorization': 'Bearer '+access_token}
json_data = {'rajNames': ['WAR']}
params = {'jsonRequest': json.dumps(json_data)}
url = 'http://258.198.39.215:8280/areas/0.1/get/raj/name'
r = requests.get(url, params=params, headers=headers)
print _r.url
Demo:
演示:
>>> import requests
>>> import json
>>> headers = {'Accept': 'application/json', 'Authorization': 'Bearer <access_token>'}
>>> json_data = {'rajNames': ['WAR']}
>>> params = {'jsonRequest': json.dumps(json_data)}
>>> url = 'http://258.198.39.215:8280/areas/0.1/get/raj/name'
>>> requests.Request('GET', url, params=params, headers=headers).prepare().url
'http://258.198.39.215:8280/areas/0.1/get/raj/name?jsonRequest=%7B%22rajNames%22%3A+%5B%22WAR%22%5D%7D'
You can still eliminate the spaces used in the JSON output from json.dumps()
by setting the separators
argument to (',', ':')
:
您仍然可以json.dumps()
通过将separators
参数设置为来消除 JSON 输出中使用的空格(',', ':')
:
>>> json.dumps(json_data)
'{"rajNames": ["WAR"]}'
>>> json.dumps(json_data, separators=(',', ':'))
'{"rajNames":["WAR"]}'
but I doubt that is really needed.
但我怀疑这是否真的需要。