命中 API 时 curl 与 python“请求”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31061227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:24:33  来源:igfitidea点击:

curl vs python "requests" when hitting APIs

pythoncurlpython-requestsbitbucket-api

提问by codyc4321

I am trying to hit the Bitbucket API for my account, and a successful attempt looks like:

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

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

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

in the command line. In python, I try:

在命令行中。在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"})

all get 405 error. The API is https://confluence.atlassian.com/bitbucket/rest-apis-222724129.html.

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

I wonder:

我想知道:

  1. What am I doing wrong in the requests version, they all look similar to my curl attempt

  2. What is the difference between requesting with curl and python requests module? What general pattern can I recognize when reading an API with a curl example and then writing it in python?

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

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

Thank you

谢谢

ANSWER:

回答:

it required the right headers

它需要正确的标题

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)

采纳答案by jlhonora

Here's a way to do basic HTTP auth with Python's requests module:

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

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

With the other way you're passing the user/pass through the request's payload, which is not desired since HTTP basic auth has its own place in the HTTP protocol.

使用另一种方式传递用户/传递请求的有效负载,这是不希望的,因为 HTTP 基本身份验证在 HTTP 协议中有自己的位置。

If you want to "see" what's happening under the hood with your request I recommend using httpbin:

如果您想根据您的请求“查看”幕后发生的事情,我建议您使用 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"
}

And with curl:

和卷曲:

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"
}

Notice the resemblance between the last python example and the cURL one.

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

Now, getting right the API's format is another story, check out this link: https://answers.atlassian.com/questions/94245/can-i-create-a-bitbucket-repository-using-rest-api

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

The python way should be something like this:

python方式应该是这样的:

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

回答by DaWe

With python3, you can use json={...} instead of data={...}, and it will set the header automatically to application/json:

在 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']