Getting a JSON request in a view (using Django)
I am trying to set up a view to received a JSON notification from an API. I'm trying to figure out how to get the JSON data, and I currently have this as a starting point to see that the request is being properly received:
def api_response(request):
print request
return HttpResponse('')
I know the JSON object is there because in the print request
it shows:
META:{'CONTENT_LENGTH': '178',
[Fri Sep 09 16:42:27 2011] [error] 'CONTENT_TYPE': 'application/json',
However, both of the POST and GET QueryDicts are empty. How would I set up a view to receive the JSON object so I can process it? Thank you.
Solution 1:
This is how I did it:
def api_response(request):
try:
data=json.loads(request.raw_post_data)
label=data['label']
url=data['url']
print label, url
except:
print 'nope'
return HttpResponse('')