访问 API 时,curl 与 python 的“请求”比较

2024-03-14

我正在尝试为我的帐户访问 Bitbucket API,成功的尝试如下所示:

curl --user screename:mypassword https://api.bitbucket.org/1.0/user/repositories

在命令行中。在Python中,我尝试:

import requests
url = 'https://api.bitbucket.org/1.0/user/repositories'

then

r = requests.post(url, data={'username': myscreename, 'password':mypassword})

and

r = requests.post(url, data="myscreename:mypassword")

and

r = requests.post(url, data={"user": "myscreename:mypassword"})

全部收到 405 错误。 API 是https://confluence.atlassian.com/bitbucket/rest-apis-222724129.html https://confluence.atlassian.com/bitbucket/rest-apis-222724129.html.

我想知道:

  1. 我在请求版本中做错了什么,它们看起来都与我的卷曲尝试类似

  2. 使用curl请求和python requests模块有什么区别?当使用curl示例读取API然后用python编写它时,我可以识别什么一般模式?

谢谢

ANSWER:

它需要正确的标题

https://answers.atlassian.com/questions/18451025/answers/18451117?flashId=-982194107 https://answers.atlassian.com/questions/18451025/answers/18451117?flashId=-982194107

UPDATE:

# ===============
# get user
# ===============
import requests
import json

# [BITBUCKET-BASE-URL], i.e.: https://bitbucket.org/
url = '[BITBUCKET-BASE-URL]/api/1.0/user/'
headers = {'Content-Type': 'application/json'}

# get user
# [USERNAME], i.e.: myuser
# [PASSWORD], i.e.: itspassword
r = requests.get(url, auth=('[USERNAME]', '[PASSWORD]'), headers=headers)
print(r.status_code)
print(r.text)
#print(r.content)

以下是使用 Python 的 requests 模块进行基本 HTTP 身份验证的方法:

requests.post('https://api.bitbucket.org/1.0/user/repositories', auth=('user', 'pass'))

使用另一种方式,您可以通过请求的有效负载传递用户/密码,这是不需要的,因为 HTTP 基本身份验证在 HTTP 协议中拥有自己的位置。

如果您想“查看”您的请求的幕后情况,我建议使用 httpbin:

>>> url = "http://httpbin.org/post"
>>> r = requests.post(url, data="myscreename:mypassword")
>>> print r.text
{
  "args": {}, 
  "data": "myscreename:mypassword", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "22", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.5.1 CPython/2.7.6 Darwin/14.3.0"
  }, 
  "json": null, 
  "origin": "16.7.5.3", 
  "url": "http://httpbin.org/post"
}

>>> r = requests.post(url, auth=("myscreename", "mypassword"))
>>> print r.text
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Authorization": "Basic bXlzY3JlZW5hbWU6bXlwYXNzd29yZA==", 
    "Content-Length": "0", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.5.1 CPython/2.7.6 Darwin/14.3.0"
  }, 
  "json": null, 
  "origin": "16.7.5.3", 
  "url": "http://httpbin.org/post"
}

并用卷曲:

curl -X POST --user myscreename:mypassword http://httpbin.org/post
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Authorization": "Basic bXlzY3JlZW5hbWU6bXlwYXNzd29yZA==", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.37.1"
  }, 
  "json": null, 
  "origin": "16.7.5.3", 
  "url": "http://httpbin.org/post"
}

请注意最后一个 Python 示例与 cURL 示例之间的相似之处。

现在,正确处理 API 的格式是另一回事了,请查看此链接:https://answers.atlassian.com/questions/94245/can-i-create-a-bitbucket-repository-using-rest-api https://answers.atlassian.com/questions/94245/can-i-create-a-bitbucket-repository-using-rest-api

python 的方式应该是这样的:

requests.post('https://api.bitbucket.org/1.0/repositories', auth=('user', 'pass'), data = "name=repo_name")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

访问 API 时,curl 与 python 的“请求”比较 的相关文章

随机推荐