convert requests.models.Response to Django HttpResponse
This should works:
from django.http import HttpResponse
import requests
requests_response = requests.get('/some-url/')
django_response = HttpResponse(
content=requests_response.content,
status=requests_response.status_code,
content_type=requests_response.headers['Content-Type']
)
return django_response
To add to Brian Loughnane's answer: when I tried the solution:
for k, v in requests_response.headers.items():
django_response[k] = v
I got an error from django: AssertionError: Hop-by-hop headers not allowed
I don't know if it's the best solution but I "fixed" it by removing the offending headers.
from wsgiref.util import is_hop_by_hop
for k, v in requests_response.headers.items():
if not is_hop_by_hop(k):
django_response[k] = v