Mehdi Soheili
Mehdi Soheili

Reputation: 17

Python Requests POST - 400 - Bad Request with Python requests

I'm to send request to a server from my app. I'm trying to create a contact and send it to web server with requests.post(). i trying server response by curl and all things fine and server retrun 200. i use reqbin.com/curl to test server response i send to server this curl bash commands

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

by above command i got 200 ok response from server i try to send request by my python client code:

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)

i test several types of send requests but allways i got 400 bad request from server. i try this but same result!

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!

any body knows whats problem?

Upvotes: 0

Views: 2779

Answers (1)

Z4-tier
Z4-tier

Reputation: 7978

change your payload to this:

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
}

and then send it with:

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

I think your data types are not matching what the server is expecting:

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

It's safest to build your python objects using the correct python semantics and then let json.dumps handle the translation, instead of trying to force it by using strings.

Upvotes: 3

Related Questions