'POST request can't pass body data from python

i am trying to post a request with body to flask REST-api but it doesn't work (Error Response 500), but when i am trying to post it from POSTMAN it works.

  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)

i tried with json.dump() also but it was the same result.

POSTMAN CODE (python) that works:

payload = "{\r\n      \"content_type\": \"test\",\r\n      \"dc\":{\r\n        \"title\": \" test\",\r\n        \"is_top\": test,\r\n        \"position\": 0,\r\n        \"status\" : \"test\"\r\n      }\r\n    }"
headers = {'Content-Type': 'application/json'}

response = requests.request("POST", url, headers=headers, data=payload)


Solution 1:[1]

Try this:

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

Solution 2:[2]

instead json.dump use 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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 at54321
Solution 2