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"<\/a>
Make sure you a passing the content type header during your post and put request, as that how django_rest_framework understand which parser it will use. django rest framework supports content type of form-data, json, x-www-form-urlencoded and multipart out of the box
r =request.post(url=URL, data=json_data,headers={ 'Content-Type':'application/json'})
TL;DR: You need to pass the payload as json<\/code> argument in
requests.post<\/code> :
data = {
"name": "sa",
"roll": 1,
"city": "lam"
}
r = requests.post(url=URL, json=data)
import json
import requests
url = "https://127.0.0.1:8000/api/studentapi/"
data_response = {
"name": "sa",
"roll": 1,
"city": "lam"
}
data_json = json.dumps(data_response)
req = requests.post(url, data=data_json)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.