Python 向请求模块添加头
前面我使用了Python 向请求模块添加头,python,http-headers,python-requests,Python,Http Headers,Python Requests,前面我使用了httplib模块在请求中添加一个头。现在,我正在对请求模块进行同样的尝试 这是我正在使用的python请求模块: 如何将标题添加到request.post()和request.get()。假设我必须在标题中的每个请求中添加foobar键。From 您只需要使用头创建一个dict(key:value对,其中key是头的名称,value是对的值),然后将该dict传递给.get或或方法上的headers参数 因此,针对您的问题: headers = {'foobar': 'raboo
httplib
模块在请求中添加一个头。现在,我正在对请求
模块进行同样的尝试
这是我正在使用的python请求模块:
如何将标题添加到request.post()
和request.get()
。假设我必须在标题中的每个请求中添加foobar
键。From
您只需要使用头创建一个dict(key:value对,其中key是头的名称,value是对的值),然后将该dict传递给.get
或或方法上的headers参数
因此,针对您的问题:
headers = {'foobar': 'raboof'}
requests.get('http://himom.com', headers=headers)
您还可以这样做,为会话对象的所有未来get设置一个头,其中x-test将出现在所有s.get()调用中:
发件人:查看您发送和/或接收的回复可能会有所帮助。根据,使用r.headers
访问服务器发回的头,并使用r.request.headers
查看您发送到服务器的头。的可能重复项
headers = {'foobar': 'raboof'}
requests.get('http://himom.com', headers=headers)
s = requests.Session()
s.auth = ('user', 'pass')
s.headers.update({'x-test': 'true'})
# both 'x-test' and 'x-test2' are sent
s.get('http://httpbin.org/headers', headers={'x-test2': 'true'})