简体   繁体   中英

How to post a request with python specifying the body raw

I am trying to send a JSON object using python but I am getting 422 error. Meanwhile, with postman I am able to post the JSON via the API: First I enter the authentication in the Authorization and in the body I specify raw and choose JSON then I post the JSON object and get a 200 response.

在此处输入图像描述

But with python:

endpoint = "some endpoint"
url = host + endpoint
headers={"Accept": "application/json",
         "Authorization": f"Bearer {bearer_token}"}
        
order = json.dumps(json_object, ensure_ascii=False)
send_data = requests.post(url, json=order, headers=headers)
print(send_data.json())
        
if send_data.status_code==200:
   print("Order successfully sent")
else:
   print(f"The following error was encountered. Error: {send_data.status_code}")

What could be wrong? please advise

try

headers={"Accept": "application/json",
         "Authorization": f"Bearer {bearer_token}"}

send_data = requests.post(url, data=order, headers=headers)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM