I-love-python
I-love-python

Reputation: 75

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.

enter image description here

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

Upvotes: 0

Views: 298

Answers (1)

Nirel
Nirel

Reputation: 1925

try

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

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

Upvotes: 1

Related Questions