python - 向请求模块添加 header

标签 python http-headers python-requests

之前我使用 httplib 模块在请求中添加 header 。现在我正在用 requests 模块尝试同样的事情。

这是我正在使用的 python 请求模块: http://pypi.python.org/pypi/requests

如何向 request.post()request.get() 添加 header 。假设我必须在标题中的每个请求中添加 foobar 键。

最佳答案

来自 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)

您只需要使用标题创建一个字典(键:值对,其中键是标题的名称,值是对的值)并将该字典传递给标题上的参数.get.post 方法。

所以更具体到你的问题:

headers = {'foobar': 'raboof'}
requests.get('http://himom.com', headers=headers)

关于python - 向请求模块添加 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8685790/

相关文章:

ios - swift 2.0 与 Alamofire : How to send http headers with X-WSSE Authorization?

python - 在 python 中从 cURL GET 请求保存 .zip 文件

ubuntu - 如何在 Ubuntu 14.04 Trusty 上安装 2.4.3 版的 python-requests?

Python 导入相对路径

python - 解析装饰器中的args和kwargs

python - 检查 Windows 应用程序的 "liveness"?

powershell - Invoke-RestMethod:发送到 header 时,无法根据请求找到身份验证数据

python - 如何在 Python 中按顺序计算两个单词之间的常用字母?

python - 如何根据 google chrome 检查器提供的信息发出 xml http post 请求