django-rest-framework accept JSON data?

Issue

I have created RESTFul APIs using . The user endpoint is: /api/v1/users

I want to create a new user, so I send the user data in JSON format:

{
    "username": "Test1",
    "email": "[email protected]",
    "first_name": "Test1",
    "last_name": "Test2",
    "password":"12121212"
}

I am using Google Chrome extension Postman to test the API. But, after sending the request, the user data is not saving. The response contains this error:

{
    "detail": "Unsupported media type \"text/plain;charset=UTF-8\" in request."
}

This is what the request details look like in Postman:

Postman API request to django-rest-framework error

Solution

You have missed adding the Content-Type header in the headers section. Just set the Content-Type header to application/json and it should work.

See the below image:

postman

Also, you might also need to include a CSRF token in the header in case you get an error {"detail": "CSRF Failed: CSRF token missing or incorrect."} while making a POST request using Postman. In that case, add an X-CSRFToken header also with value as the CSRF token value.

Answered By – Rahul Gupta

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published