将标头添加到python请求模块 [英] adding header to python request module

查看:133
本文介绍了将标头添加到python请求模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前我使用 httplib 模块在请求中添加标头。现在我正在尝试使用请求模块。

Earlier I used httplib module to add header in the request. Now I am trying same thing with the request 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 header to the request.post and request.get say I have to add foobar key in each request in 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)

你只需要用你的标题创建一个dict(键:值对,其中键是标题的名称,值是,值,值of他配对)并将该dict传递给 .get .post 方法的headers参数。

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.

更具体的问题:

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

这篇关于将标头添加到python请求模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆