pythonpostpython-requestsputhttp-status-code-415

Unsupported Media Type while PUT


I have a DRF server that accepts the following renders and parsers:

    "renders": [
    "application/json",
    "text/html"
],
"parses": [
    "multipart/form-data",
    "application/x-www-form-urlencoded"
],

When I am POSTing an image to .../snippets/, I receive 200 response code.

When I am trying to POST a JSON Object, I receive 415 response code.

Here is the snippet for POST:

Boxes = [{'X': 2423, 'Y': 532, 'W': 7456, 'H': 2345}]
res = json.dumps(Boxes)



r = requests.put(
    "http://127.0.0.1:8000/snippets/1/",
    data=json.dumps({"Boxes": res,

                     "Amount": '3'
                     }),
    headers={'Authorization': 'Token 7095dcc10e57301518f192a4e3a0fb275157cb2f', 'Content-Type': 'application/json'},
)
print(r.status_code)

I am able to make a POST request using Postman so, definitely I am missing something here.

I will appreciate any help


Solution

  • The issue was with the 'Content-Type': 'application/json'. In my case I need to use 'Content-Type': 'application/x-www-form-urlencoded' and everything works just fine