[英]Converting curl command to Python request
The curl command that I have that works properly is - 我拥有的curl命令可以正常工作-
curl -X GET -H "Authorization: Basic <base64userpass>" -H "Content-Type: application/json" "http://<host>/bamboo/rest/api/latest/result/<plankey>.json?expand=results.result&os_authType=basic"
In Python, this is what I currently have - 在Python中,这是我目前拥有的-
headers = {'Authorization': 'Basic <base64userpass>', 'Content-Type': 'application/json'}
datapoints = {'expand': 'results.result', 'os_authType': 'basic'}
url = "http://<host>/bamboo/rest/api/latest/result/<plankey>.json"
r = requests.get(url, headers=headers, data=datapoints)
The response I get when using the Python request is <Response [403]>
, but when using curl I get back the expected data. 使用Python请求时得到的响应是
<Response [403]>
,但是使用curl时,我得到了预期的数据。
What am I missing here? 我在这里想念什么?
Thanks. 谢谢。
You should use the auth
option of requests to do basic authentication. 您应该使用请求的
auth
选项进行基本身份验证。 There are more headers that the CURL command-line handle for you (and requests will not handle them unless you use the auth
): CURL命令行可以为您处理更多的标头(除非您使用
auth
否则请求将不会处理它们):
>>> from requests.auth import HTTPBasicAuth
>>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))
Or just use: 或者只是使用:
>>> requests.get('https://api.github.com/user', auth=('user', 'pass'))
(Change the URL and everything). (更改URL和所有内容)。
Also note that requests
should get the params=
(and not data=
). 另请注意,
requests
应获取params=
(而不是data=
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.