I feel like I'm missing something terribly obvious, but I can't see it.
In ipython (Python 2.7), this works:
In [1]: json.loads('[]')
Out[1]: []
I'm trying to do the equivalent minimal example in a Django view, to convince myself I can read JSON before moving on to a real data structure:
def submit(request):
json_string = request.GET['json']
parsed = json.loads(json_string)
return HttpResponse(parsed)
I call it from my browser by going to
http://localhost:8001/submit/?json='[]'
And I get ValueError: No JSON object could be decoded
.
What am I doing wrong, and how do I debug it? Is this some kind of encoding problem? Or am I accessing the content of the GET incorrectly? Or something else?
I've tried dumping the variable json_string
, both to a file and as an HttpResponse. Both times I get '[]'
, which sure looks fine...
Edit:
For the benefit of any other beginners wondering how to debug a POST, Django's error page is returned in the exception. A quick and nasty hack that works is
import os
def debug_post(post_req):
result = None
try:
result = submit_to_endpoint(post_req)
except urllib2.URLError as e:
out_fh = open('debug_post.tmp', 'w')
out_fh.write(e.read())
out_fh.close()
os.system('firefox ' + 'debug.tmp')
return result