[英]Converting from curl to python request.post
我希望將我當前的 curl 命令轉換為 python 代碼以請求 api 的訪問令牌。
curl -X POST -H "Content-Type:application/json" -d '{"grantType":"client_credentials"}' [Access Token URL]
我的嘗試:
import requests
api_url_base = "https://api.example.com/api/"
headers ={'Content_Type':'application/json',
'grandType': 'client_credentials'
}
response = requests.post(headers=headers, "https://api.example.com/accesstokens")
if response.status_code == 200:
print(json.loads(response.content.decode('utf-8')))
else:
print(response.status_code)
預期輸出:應該在 python 中。
您在那里發布的標題和正文數據不匹配,更不用說grandType
有錯字了。
無論哪種方式,使用 Requests 發布 JSON 和解析 JSON 響應都非常簡單:
response = requests.post(
"https://api.example.com/accesstokens",
json={"grantType": "client_credentials"},
)
response.raise_for_status() # raise an exception for error statuses
data = response.json()
print(data)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.