-1

I'm trying to reproduce the code from this page, the complete github code is here:

The application works just fine on the browser, but I'm not able of reproduce the POST request from python.

I have tried with the same data showed on the payload when I use the browser

enter image description here

PEOPLE =  {"fname": "DDoug",
        "lname": "FarDrell"}

url = "http://localhost:5000/api/people"
data = requests.post(url,data=json.dumps(PEOPLE) )

but I get the following errror:

data.text

'{\n  "detail": "Invalid Content-type (), expected JSON data",\n  "status": 415,\n  "title": "Unsupported Media Type",\n  "type": "about:blank"\n}\n'

I tried like this as well:

url = "http://localhost:5000/api/people"
data = requests.post(url,data=json.dumps(PEOPLE) )

BUT got this error:

'{\n  "detail": "Invalid Content-type (application/x-www-form-urlencoded), expected JSON data",\n  "status": 415,\n  "title": "Unsupported Media Type",\n  "type": "about:blank"\n}\n'
Luis Ramon Ramirez Rodriguez
  • 9,591
  • 27
  • 102
  • 181

1 Answers1

4

Add the Content-Type into your post headers to specify you're sending JSON data:

requests.post(url,data=json.dumps(PEOPLE), headers={'Content-Type': 'application/json'})

You can also use the json parameter to achieve the same result:

requests.post(url, json=json.dumps(PEOPLE))
djnz
  • 2,021
  • 1
  • 13
  • 13