Issue
Hey I am new to Django Rest Framework.I have recently created REST API name "api/studentapi" using django REST framework in local host. The CRUD operation works perfectly fine in browseable API. But When I try to access the same API from third party python file, I am not able to perform POST operation although I am able to perform GET operation.While performing POST operation the server reponse me as "Unsupported Media Type: /api/studentapi/".I am posting series of code images so that the stuffs gets more clear The third party python app that I have created to access the "api/studentapi"
The file structure of my project
The model form that I have created
The serializer files that I have created
Solution
Make sure you a passing the content type header during your post and put request, as that's how django_rest_framework
understands which parser it will use. The Django rest framework supports content type of form-data
, JSON
, x-www-form-urlencoded
and multipart
out of the box.
If you're sending JSON data add the header to your request:
Content-Type: application/json
If you send form data or file(s) add the header:
Content-Type: multipart/form-data
If you're sending only form data use:
Content-Type: application/x-form-urlencoded
For your code this is how to add the header
r = request.post(
url=URL,
data=json_data,
headers= {
'Content-Type': 'application/json'
}
)
Answered By - Innocent Peros
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.