簡體   English   中英

Python 請求 POST - 400 - 帶有 Python 請求的錯誤請求

[英]Python Requests POST - 400 - Bad Request with Python requests

我要從我的應用程序向服務器發送請求。 我正在嘗試創建一個聯系人並使用 requests.post() 將其發送到 web 服務器。 我嘗試通過 curl 進行服務器響應,一切正常,服務器重新運行 200。我使用 reqbin.com/curl 測試服務器響應我發送到服務器這個 curl 命令 ZD574D4BB40C84861791A6949A

curl 'https://axxxxxxxxxxxxe.com/xx/xx/xxx/Post' \
-H 'authority: axxxxxxxxxxxxe.com' \
-H 'authorization: BasicAuthentication 45442f41-09a5-42b8-8808-e1cd49739bbe' \
-H 'user-agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/87.0.4280.88 Safari/537.36' \
-H 'content-type: application/json' \
-H 'accept: */*' \
-H 'origin: https://www.xxxx.com' \
-H 'sec-fetch-site: same-site' \
-H 'sec-fetch-mode: cors' \
-H 'sec-fetch-dest: empty' \
-H 'referer: https://www.xxxx.com/' \
-H 'accept-language: en-US,en;q=0.9,xx;q=0.8,xx;q=0.7' \
--data-binary     '{"IsSxxxxxxment":false,"Caxxxxxxted":false,"Isxxxxent":false,"Sexxxxted":false,"oxxxt":1000,"oxxxxe":311    10,"FxxxxxerId":1,"mixxxxy":0,"maxxxw":0,"orxxxd":0,"ixxn":"Ixxxxx0001","oxxxxde":65,"orxxxxity":74,"orxx    date":null,"shxxxxled":false,"sxoxxnt":0}' \
--compressed

通過上面的命令,我得到了來自服務器的 200 ok 響應,我嘗試通過我的 python 客戶端代碼發送請求:

url="https://axxxxxxxxxxxxe.com/xx/xx/xxx/Post"
headers = {
"authority" : "axxxxxxxxxxxxe.com",
"authorization" : "BasicAuthentication 45442f41-09a5-42b8-8808-e1cd49739bbe",
"user-agent" : "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
"content-type" : "application/json",
"accept" : "*/*",
"origin" : "https://www.xxxx.com",
"sec-fetch-site" : "same-site",
"sec-fetch-mode" : "cors",
"sec-fetch-dest" : "empty",
"referer" : "https://www.xxxx.com/",
"accept-language" : "en-US,en;q=0.9,xx;q=0.8,xx;q=0.7"
}
payload = {
"IsSxxxxxxment" : "false",
"Caxxxxxxted" : "false",
"Isxxxxent" : "false",
"Sexxxxted" : "false",
"oxxxt" : "10000",
"oxxxxe" : "9762", 
"FxxxxxerId" : "1",
"mixxxxy" : "0",
"maxxxw" : "0",
"orxxxd" : "0",
"ixxn" : "Ixxxxx0001",
"oxxxxde" : "65",
"orxxxxity" : "74",
"orxxdate" : "null",
"shxxxxled" : "false",
"sxoxxnt" : "0"
}
response = requests.post(url , data=payload, headers=headers) #return 400
#response = requests.post(url , json=payload, headers=headers) #return 400
#response = requests.post(url , data=payload, headers=headers, verify=False)) #return 400
#response = requests.post(url , json=payload, headers=headers, verify=False)) #return 400
#response = requests.post(url , data=json.dumps(payload), headers=headers) #return 400
print (response.status_code) 
print (response.reason)

我測試了幾種類型的發送請求,但總是收到來自服務器的 400 個錯誤請求。 我試試這個,但結果相同!

payload = {
"IsSxxxxxxment" : False,
"Caxxxxxxted" : False,
"Isxxxxent" : False,
Sexxxxted" : False,
"oxxxt" : 10000,
"orderPrice" : 9762, 
"FxxxxxerId" : 1,
"mixxxxy" : 0,
"maxxxw" : 0,
"orxxxd" : 0,
"ixxn" : "IRO7KMOP0001",
"oxxxxde" : 65,
"orxxxxity" : 74,
"orxxdate" : "null",
"shxxxxled" : False,
"sxoxxnt" : 0
}
response = requests.post(url , data=payload, headers=headers) #return 400!

任何機構都知道什么問題?

將您的有效負載更改為:

payload = {
"IsSxxxxxxment" : False,
"Caxxxxxxted" : False,
"Isxxxxent" : False,
"Sexxxxted" : False,
"oxxxt" : 10000,
"oxxxxe" : 9762, 
"FxxxxxerId" : 1,
"mixxxxy" : 0,
"maxxxw" : 0,
"orxxxd" : 0,
"ixxn" : "Ixxxxx0001",
"oxxxxde" : 65,
"orxxxxity" : 74,
"orxxdate" : None,
"shxxxxled" : False,
"sxoxxnt" : 0
}

然后發送:

response = requests.post(url , data=json.dumps(payload), headers=headers)

我認為您的數據類型與服務器的期望不匹配:

  • json null != "null"
  • json 0 != "0"
  • json false != "false"

使用正確的 python 語義構建 python 對象是最安全的,然后讓json.dumps處理翻譯,而不是嘗試通過使用字符串來強制翻譯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM