I have created RESTFul APIs using django-rest-framework. The user endpoint is
/api/v1/users
I want to create new user. I send user data in JSOn format.
{
"username": "Test1",
"email": "test1@gmail.com",
"first_name": "Test1",
"last_name": "Test2",
"password":"12121212"
}
I am using Chrome extension Postman to test api. But user data has not been saving. The response is:
{
"detail": "Unsupported media type \"text/plain;charset=UTF-8\" in request."
}
Attached screenshot
You have missed adding the
Content-Type
header in the headers section. Just set theContent-Type
header toapplication/json
and it should work.See the below image:
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 aPOST
request using Postman. In that case, add anX-CSRFToken
header also with value as the CSRF token value.You need to define content type by setting the appropriate headers. In case of Postman you need to set the following values under url field:
Header: "Content-Type"
Value: application/json
I'm posting this answer in case someone is facing a problem like mine.
I'm working on a Front-End app using Angular 2 with an API made with Django Rest Framework and I used to send requests with the following headers:
And it was working fine until I tried it on Fire Fox and I couldn't load the needed data and I solved it with adding the following headers
Here's an explanation,
Content-Type
tells the server what is the content type of data is whileAccept
tells it what content type the client side will accpet.Here's a nice clear answer about this issue:
You need to do two step to done this issue:
Content-Type
header withapplication/json
valueAuthorization
header withToken {YOUR_CUSTOM_TOKEN}
value to pass CSRFTokenNote: if you want to authenticate with session, you don't need to do second step, but if you want use this API for mobile, you have to pass Authorization header to server
I hope it helps