Python 请求 POST - 400 - 带有 Python 请求的错误请求

时间:2020-12-19 21:38:12

标签: python

我要从我的应用程序向服务器发送请求。 我正在尝试创建一个联系人并使用 requests.post() 将其发送到 Web 服务器。 我尝试通过 curl 进行服务器响应,一切正常,服务器重新运行 200。 我使用 reqbin.com/curl 来测试服务器响应 我将此 curl bash 命令发送到服务器

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!

有人知道什么问题吗?

1 个答案:

答案 0 :(得分:1)

将您的有效负载更改为:

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 处理翻译是最安全的,而不是试图通过使用字符串来强制它。