POST 请求无法从 python 传递正文数据



我试图将带有body的请求发布到烧瓶RESTapi,但它不起作用(错误响应500(,但当我试图从POSTMAN发布它时,它起作用。

body = {
"content_type": "test",
"dc":{
"title": " test",
"is_top": "test",
"position": 0,
"status" : "test"
}
}
headers = {
'Content-Type': 'application/json', 'Accept': 'text/plain'
}
response = requests.post(url, headers = headers, data=body)

我也试过用json.dump(),但结果是一样的。

POSTMAN代码(python(工作:

payload = "{rn      "content_type": "test",rn      "dc":{rn        "title": " test",rn        "is_top": test,rn        "position": 0,rn        "status" : "test"rn      }rn    }"
headers = {'Content-Type': 'application/json'}
response = requests.request("POST", url, headers=headers, data=payload)

试试这个:

body = {
"content_type": "test",
"dc": {
"title": " test",
"is_top": "test",
"position": 0,
"status": "test"
}
}
resp = requests.post(url, json=body)
resp.raise_for_status()

替代json.dump使用json.dumps

import requests
import json
postData = { "id" : 1 , "limit":False, "list":[1,2,3] }
response = requests.post("https://abc/api/method",data=json.dumps(postData))
print(json.loads(response.content)

最新更新