curl vs python“请求”当击中API [英] curl vs python "requests" when hitting APIs

查看:237
本文介绍了curl vs python“请求”当击中API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的帐户点击Bitbucket API,并且成功的尝试如下:

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

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

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

in the command line. In python, I try:

import requests
s = requests.session()
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/display/ BITBUCKET / Use + the + Bitbucket + REST + APIs

all get 405 error. The API is https://confluence.atlassian.com/display/BITBUCKET/Use+the+Bitbucket+REST+APIs

我不知道:


  1. 请求版本中我做错了,它们都与我的卷曲尝试类似

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

与curl和python请求模块?

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?

谢谢你,我们在使用curl例子读取API时,

Thank you

ANSWER:

它需要正确的标题

https://answers.atlassian.com/questions/18451025/ answer / 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的请求模块进行基本的HTTP auth:

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

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

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

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.

如果你想看到发生在你的要求下发生的事情,我建议使用httpbin:

If you want to "see" what's happening under the hood with your request I recommend using 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一个。

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

现在,获得API的格式是另一个故事,请查看此链接: https://answers.atlassian.com/questions/94245/cani-i- create-a-bitbucket-repository-using-rest-api

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

python方法应该是这样:

The python way should be something like this:

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

这篇关于curl vs python“请求”当击中API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆