curl与python在命中api时的“请求”

2024-04-18 06:53:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为我的帐户使用Bitbucket API,成功的尝试如下:

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

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

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

那么

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

以及

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

以及

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

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

我想知道:

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

  2. curl请求和python请求模块有什么区别?在用curl示例读取API并用python编写它时,我可以识别什么样的通用模式?

谢谢你

回答:

它需要正确的标题

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

更新:

# ===============
# 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)

Tags: httpsorgimportapiurlbitbucketdataget
1条回答
网友
1楼 · 发布于 2024-04-18 06:53:45

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

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

另一种方法是传递用户/传递请求的有效负载,这是不需要的,因为HTTP basic auth在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

python的方式应该是这样的:

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

相关问题 更多 >