Skip to content

Instantly share code, notes, and snippets.

@bblanchon
Created April 26, 2021 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bblanchon/576d8acc58f59358855659104c0d461d to your computer and use it in GitHub Desktop.
Save bblanchon/576d8acc58f59358855659104c0d461d to your computer and use it in GitHub Desktop.
Django HTTP proxy response / proxy view
import requests
from django.http import StreamingHttpResponse
class ProxyHttpResponse(StreamingHttpResponse):
def __init__(self, url, headers=None, **kwargs):
upstream = requests.get(url, stream=True, headers=headers)
kwargs.setdefault('content_type', upstream.headers.get('content-type'))
kwargs.setdefault('status', upstream.status_code)
kwargs.setdefault('reason', upstream.reason)
super().__init__(upstream.raw, **kwargs)
for name, value in upstream.headers.items():
self[name] = value
def my_proxy_view(request):
url = request.GET['url']
return ProxyHttpResponse(url, headers=request.headers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment