I am trying to send JSON data to my django web app using python urllib but with no luck. This is my code;
Python application:
url = "http://127.0.0.1:8000/web_app"
values = {'name':'Paul','age':12}
jdata = {'data':values}
data = urllib.urlencode(values)
req = urllib2.Request(url, data, {'Content-Type':'application/json'})
try:
resp = urllib2.urlopen(req)
the_result = resp.read()
except urllib2.HTTPError, e:
return "Reques Failed!"
My Django Web App:
@csrf_exempt
def web_app(request):
print "In My webapp" # This never get printed!
data = request.POST['data']
return HttpResponse("Thankyou...")
The request seems not to be hitting the Django web_app function, because the first print in the function is not executed!
Added: note if i remove the data from the request in the urllib2.request then everything works as expected!!
What am i missing?