Django: How to return a raw response
original source: https://stackoverflow.com/questions/7952518/django-how-to-return-a-raw-response
You shouldn’t return the response from urlopen method directly. Instead your view should return an instance of django’s HttpResponse, where body and the headers should be set to those from the original response:
from django.http import HttpResponse
import urllib2
def my_view(request):
request = urllib2.Request(url, formData, headers)
response = urllib2.urlopen(request)
# set the body
r = HttpResponse(response.read())
# set the headers
for header in response.info().keys():
r[header] = response.info()[header]
return r