[英]curl vs python "requests" when hitting APIs
我正在嘗試為我的帳戶訪問 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 。
我想知道:
我在請求版本中做錯了什么,它們看起來都與我的 curl 嘗試相似
使用 curl 和 python requests 模塊請求有什么區別? 在讀取帶有 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)
這是一種使用 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
python方式應該是這樣的:
requests.post('https://api.bitbucket.org/1.0/repositories', auth=('user', 'pass'), data = "name=repo_name")
在 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']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.