64
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:
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.
11
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:
'Content-Type': 'application/json'
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
'Content-Type': 'application/json',
'Accept': 'application/json'
Here’s an explanation, Content-Type
tells the server what is the content type of data is while Accept
tells it what content type the client side will accpet.
Here’s a nice clear answer about this issue:
- [Django]-Check for presence in a list django template
- [Django]-Django and query string parameters
- [Django]-Difference between reverse() and reverse_lazy() in Django
7
You need to do two step to done this issue:
- Add
Content-Type
header withapplication/json
value - Add
Authorization
header withToken {YOUR_CUSTOM_TOKEN}
value to pass CSRFToken
Note: 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
- [Django]-ImportError: No module named 'django.core.urlresolvers'
- [Django]-Database returned an invalid value in QuerySet.dates()
- [Django]-How to update() a single model instance retrieved by get() on Django ORM?
1
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
- [Django]-Django-rest-framework returning 403 response on POST, PUT, DELETE despite AllowAny permissions
- [Django]-How to implement FirebaseDB with a Django Web Application
- [Django]-Sending JSON using the django test client
1
Couple of things to do if you want to accept JSON Data using Django Rest Framework.
-
Make sure application/json headers are sent:
'Content-Type: application/json'
-
JSON Parser is selected in
settings.py
REST_FRAMEWORK = {
'DEFAULT_PARSER_CLASSES': [
'rest_framework.parsers.JSONParser',
],
}
- [Django]-Get list of Cache Keys in Django
- [Django]-Removing 'Sites' from Django admin page
- [Django]-Get protocol + host name from URL
0
I had to add the following to get this to work (I’m using node-fetch btw from the client side to do a POST):
supportHeaderParams: true,
headers: { "Content-Type": "application/json; charset=UTF-8" },
- [Django]-Django – Rotating File Handler stuck when file is equal to maxBytes
- [Django]-Set language within a django view
- [Django]-How do I pass template context information when using HttpResponseRedirect in Django?