簡體   English   中英

如何使用 python 發布請求並指定正文

[英]How to post a request with python specifying the body raw

我正在嘗試使用 python 發送 JSON object 但我收到 422 錯誤。 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.

在此處輸入圖像描述

但是對於 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}")

有什么問題? 請指教

嘗試

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

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

暫無
暫無

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

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