标题添加到请求模块
问题说明
之前,我使用httplib
模块在请求中添加标头.现在,我正在使用requests
模块尝试相同的事情.
Earlier I used httplib
module to add a header in the request. Now I am trying the same thing with the requests
module.
这是我正在使用的python请求模块: http://pypi.python.org/pypi/requests
This is the python request module I am using: http://pypi.python.org/pypi/requests
如何将标头添加到request.post()
和request.get()
.假设我必须在标题的每个请求中添加foobar
键.
How can I add a header to request.post()
and request.get()
. Say I have to add foobar
key in each request in the header.
正确答案
来自 http://docs .python-requests.org/en/latest/user/quickstart/
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
headers = {'content-type': 'application/json'}
r = requests.post(url, data=json.dumps(payload), headers=headers)
您只需要创建一个带有标题的字典(键:值对,其中键是标题的名称,值是该对的值),然后将该字典传递给标头上的headers参数.get
或.post
方法.
You just need to create a dict with your headers (key: value pairs where the key is the name of the header and the value is, well, the value of the pair) and pass that dict to the headers parameter on the .get
or .post
method.
因此更具体地针对您的问题:
So more specific to your question:
headers = {'foobar': 'raboof'}
requests.get('http://himom.com', headers=headers)
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcgkjgj
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
复制文件夹/文件而不修改属性?
it1352 07-15