2

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'm trying to do:

data = {'start': 13388, 'end': 133885, 'name': 'marcus0.5'}
r = requests.post(url=url, auth=auth, headers={'Accept': 'application/json'}, data=data)

and I get the following error:

>>> r.text
     u'{"status":"error","errorCode":"COMMON_UNSUPPORTED_MEDIA_TYPE","incidentId":"czEtbXNyZXBvcnRzMDQuc3RhZ2V4dHJhbmV0LmFrYW1haS5jb20jMTM3NTgxMzc3MTk4NQ==","errorMessage":"The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method.. Content type \'application/x-www-form-urlencoded;charset=UTF-8\' not supported."}'

I think it has something to do with the json, but I'm not sure what and I'm not sure how to fix it. Any ideas?

Extra info [not sure if it applies]:

I imported

import requests, django

I know the the auth is correct and I tested it with the get method

Marcus
  • 9,032
  • 11
  • 45
  • 84
  • Just a guess, you set as accept header `application/json` but your error messages says the content type is `application/x-www-form-urlencoded`. – dav1d Aug 06 '13 at 18:38

1 Answers1

4

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.

Community
  • 1
  • 1
FastTurtle
  • 2,301
  • 19
  • 19
  • u'{"status":"error","errorCode":"COMMON_BAD_REQUEST","incidentId":"czEtbXNyZXBvcnRzMDQuc3RhZ2V4dHJhbmV0LmFrYW1haS5jb20jMTM3NTgxNTYzNjUxNg==","errorMessage":"The request could not be understood by the server due to malformed syntax or missing parameters. Failed to parse request body. Request JSON body was likely malformed, or contained an unknown field.","resolution":"Check the request body for syntax errors and/or that all required query string parameters are present"}' – Marcus Aug 06 '13 at 19:00
  • 1
    Have you tried the `POST` request without using headers? If that doesn't work, then try the above but instead of sending the data directly send `json.dumps(data)`. – FastTurtle Aug 06 '13 at 19:02
  • Without headers goes back to the initial error. HEY! but json.dumps(data) works!!! Any chance you could edit your answer and explain why json.dumps(data) works? Thanks – Marcus Aug 06 '13 at 19:09
  • 1
    @user2588348 Glad to hear it works! I've updated my answer to use `json.dumps` and provided an explanation as to why. – FastTurtle Aug 06 '13 at 19:19