I'm trying to get data from API with GET query in Django. I get as response HTML-code, where the JSON is included too. It seems like Content-Type header isn't working and that's why I get HTML-code as response.
I tested to do queries with Postman:
- When I didn't have Content-Type header at all, I got same HTML-code as now.
- When I have Content-Type header as text/json, it responded with JSON-data.
Here is my code at views.py, I don't expect this to work on website yet, so I don't return/render anything
def json_search(request):
query = request.GET.get('query')
final_url = urllib2.Request('http://APIwebsite.com', None, {'Content-Type':'text/json'})
base64string = base64.encodestring('%s:%s' % ('username', 'password')).replace('\n', '')
final_url.add_header("Authorization", "Basic %s" % base64string)
json_obj = urllib2.urlopen(final_url)
decoded_data = json_obj.read()
print decoded_data
As response I get HTML-page code, with CSS and JSON-data. How I can get just only the JSON data?
Content-Typeshould beapplication/jsonrather thantext/jsonand json.loads call should work on the returned response. Also, what's the python version that you are using?