• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

标题添加到请求模块

用户头像
it1352
帮助1

问题说明

之前,我使用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.

正确答案

#1

来自 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
系列文章
更多 icon
同类精品
更多 icon
继续加载