点击API时curl vs python“requests”

时间:2015-06-25 21:37:55

标签: python curl python-requests bitbucket-api

我正在尝试为我的帐户点击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'

然后

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. 我在请求版本中做错了什么,它们看起来与我的卷曲尝试类似

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

  3. 谢谢

    解答:

    它需要正确的标题

    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)
    

2 个答案:

答案 0 :(得分:16)

这是使用Python的请求模块进行基本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

python方式应该是这样的:

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

答案 1 :(得分:0)

使用python3,您可以使用json = {...}代替data = {......,并且它将标头自动设置为application / json:

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

data = {
    'data1': 'asd',
    'data2': 'asd'
}
req = requests.post(url, auth=('user', 'password'), json = data)
data = req.json()
# data['index']