[英]Python Post request with REQUESTS/ Shiftplanning API
我正在嘗試使用以下代碼調用shiftplanning API:
import requests
url= "https://humanity.com/api/"
payload = {"key": "keyvalue", "request": { "module": "staff.login", "method": "GET","username": "myusername", "password": "mypassword"}}"
headers = {
'content-type': "application/x-www-form-urlencoded",
'cache-control': "no-cache",
}
r = requests.post(url, data = payload
, headers= headers)
r.text
我使用Postman預先測試了api,並且效果很好。 但是,郵遞員代碼創建者中的有效負載如下所示:
payload = "data={\r\n \"key\": \"keyvalue\",\r\n \"request\": {\r\n \"module\": \"staff.login\",\r\n \"method\": \"GET\",\r\n \"username\": \"myusername\",\r\n \"password\": \mypassword\"\r\n }\r\n}"
我認為,有效負載變量實際上需要格式化為字符串,如shiftplanning api中的文檔所述:
請記住,傳遞給API的所有數據都將格式化為JSON,並且其字符串值需要通過POST HTTP請求方法作為后變量'data'發送(請參見下面示例的第9行)。 另外,Content-Type還需要設置為application / x-www-form-urlencoded。
但是,無論我做什么,我總是得到api的html作為返回,而不是對http請求的json響應。 使用
r.json()
由於返回的html,除了引發錯誤外,不執行任何操作。 我也嘗試使用
json = payload
在發布請求中,這也沒有任何改變。
任何幫助和解釋表示贊賞。 我真的很想了解我在做錯什么,因為它似乎很容易執行。
如果要json響應,請將headers
content-type
更改為application/json
。 郵遞員會自動為您設置該標頭,但您需要在Python代碼中進行設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.