Bar*_*t C 10 python python-3.x python-requests
我有一个Rails 4应用程序,它使用基于令牌的API身份验证,并且需要能够通过Python 3脚本更新记录.
我当前的脚本看起来像这样
import requests
import json
url = 'http://0.0.0.0:3000/api/v1/update_experiment.json'
payload = {'expt_name' : 'A60E001', 'status' : 'done' }
r = requests.patch(url, payload)
Run Code Online (Sandbox Code Playgroud)
如果我禁用API身份验证,它的工作正常.
我无法弄清楚如何向其添加标题,requests.patch根据文档只需要两个参数.
我需要达到添加以下标题信息的程度
'Authorization:Token token="xxxxxxxxxxxxxxxxxxxxxx"'
Run Code Online (Sandbox Code Playgroud)
这种类型的标题在curl中正常工作.我如何在Python 3和请求中执行此操作?
Pad*_*ham 12
补丁需要kwargs,只需传递headers = {your_header}:
def patch(url, data=None, **kwargs):
"""Sends a PATCH request.
:param url: URL for the new :class:`Request` object.
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
return request('patch', url, data=data, **kwargs)
Run Code Online (Sandbox Code Playgroud)
像这样的东西:
head = {"Authorization":"Token token=xxxxxxxxxxxxxxxxxxxxxx"}
url = 'http://0.0.0.0:3000/api/v1/update_experiment.json'
payload = {'expt_name' : 'A60E001', 'status' : 'done' }
r = requests.patch(url, payload, headers=head)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16283 次 |
| 最近记录: |