Ste*_*fan 5 python python-requests
我正在尝试向具有两个具有相同名称但不同值的标头的服务器发送get请求:
url = 'whatever'
headers = {'X-Attribute':'A', 'X-Attribute':'B'}
requests.get(url, headers = headers)
Run Code Online (Sandbox Code Playgroud)
这显然不起作用,因为标题字典不能包含两个键X-Attribute.
有什么我可以做的,即我可以将标题作为字典之外的其他内容传递吗?以这种方式发送请求的要求是服务器的一项功能,我无法对其进行更改.
请求使用urllib2.urlencode(或类似的东西)来编码标题.
这意味着可以将元组列表作为有效负载参数而不是字典发送,从而将字典列表从字典强加的唯一键约束中释放出来.urlib2.urlencode文档中描述了发送元组列表. http://docs.python.org/2/library/urllib.html#urllib.urlencode
以下代码将解决问题,而不会扁平或脏乱:
url = 'whatever'
headers = [('X-Attribute', 'A'),
('X-Attribute', 'B')]
requests.get(url, headers = headers)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6558 次 |
| 最近记录: |