Created
April 26, 2021 10:50
-
-
Save bblanchon/576d8acc58f59358855659104c0d461d to your computer and use it in GitHub Desktop.
Django HTTP proxy response / proxy view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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