Django Calls With Json. Common_Unsupported_Media_Type
I'm trying to make a RESTful api call in python/django with requests.post I can get requests.get(url=url, auth=auth) to work. Similar call in the same api family for this company I
Solution 1:
You want to set the Content-Type parameter of your request to 'application/json'
, not the Accept parameter.
Taken from w3.org:
The Accept request-header field can be used to specify certain media types which are acceptable for the response.
Try this instead:
import json
data = {'start': 13388, 'end': 133885, 'name': 'marcus0.5'}
r = requests.post(url=url, auth=auth, data=json.dumps(data),
headers={'content-type': 'application/json'})
EDIT:
There is a little bit of confusion (for me as well) about when to send data as a dict
or a json encoded string (ie. the result of json.dumps
). There is an excellent post here that explains the problem. For a brief summary send a dict
when the API requires form-encoded data, and a json encoded string when it requires json-encoded data.
Post a Comment for "Django Calls With Json. Common_Unsupported_Media_Type"