我有一个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)
如果我禁用API身份验证,则可以正常运行。
我无法弄清楚如何向其添加标头,requests.patch
根据文档仅需要两个参数。
我需要弄清楚将添加以下标题信息的地步
'Authorization:Token token="xxxxxxxxxxxxxxxxxxxxxx"'
这种类型的标头可以在curl中正常工作。如何在Python 3和请求中执行此操作?
补丁需要花点时间,只需传递标头= {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)
像这样的东西:
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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句