Skip to content

Instantly share code, notes, and snippets.

@dec1985
Forked from JustinTArthur/reverse_proxy_view.py
Created April 6, 2018 08:13
Show Gist options
  • Select an option

  • Save dec1985/c937a09375eeed5a5c0d0d7813017097 to your computer and use it in GitHub Desktop.

Select an option

Save dec1985/c937a09375eeed5a5c0d0d7813017097 to your computer and use it in GitHub Desktop.

Revisions

  1. @JustinTArthur JustinTArthur revised this gist Oct 21, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion reverse_proxy_view.py
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ def reverse_proxy(request):
    import requests
    requestor = getattr(requests, request.method.lower())

    proxied_response = requestor(url, data=request.REQUEST, files=request.FILES)
    proxied_response = requestor(url, data=request.body, files=request.FILES)

    from django.http.response import HttpResponse
    response = HttpResponse(proxied_response.content, content_type=proxied_response.headers.get('content-type'))
  2. @JustinTArthur JustinTArthur revised this gist Jun 4, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion reverse_proxy_view.py
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    @csrf_exempt
    def reverse_proxy(request):
    """
    Reverse proxy for a remote service that is useful for local testing.
    Reverse proxy for a remote service.
    """
    path = request.get_full_path()
    #Optionally, rewrite the path to fit whatever service we're proxying to.
  3. @JustinTArthur JustinTArthur revised this gist Jun 4, 2013. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions reverse_proxy_view.py
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,21 @@
    from django.views.decorators.csrf import csrf_exempt

    @csrf_exempt
    def reverse_proxy(request):
    from django.http.response import HttpResponse
    import requests
    """
    Reverse proxy for a remote service. To be used for local development/testing. Otherwise, consider nginx.
    Reverse proxy for a remote service that is useful for local testing.
    """
    path = request.get_full_path()
    #Optionally, rewrite the path to fit whatever service we're proxying to.

    url = "http://%s%s" % ("my_server.lol:9200", path)

    requestor = getattr(requests,request.method.lower())
    import requests
    requestor = getattr(requests, request.method.lower())

    proxied_response = requestor(url, data=request.REQUEST, files=request.FILES)

    from django.http.response import HttpResponse
    response = HttpResponse(proxied_response.content, content_type=proxied_response.headers.get('content-type'))
    for header_key, header_value in proxied_response.headers.iteritems():
    response[header_key] = header_value
  4. @JustinTArthur JustinTArthur renamed this gist Jun 4, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @JustinTArthur JustinTArthur revised this gist Jun 4, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion views.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    @csrf_exempt
    def search_api(request):
    def reverse_proxy(request):
    from django.http.response import HttpResponse
    import requests
    """
  6. @JustinTArthur JustinTArthur created this gist Jun 4, 2013.
    19 changes: 19 additions & 0 deletions views.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    @csrf_exempt
    def search_api(request):
    from django.http.response import HttpResponse
    import requests
    """
    Reverse proxy for a remote service. To be used for local development/testing. Otherwise, consider nginx.
    """
    path = request.get_full_path()
    #Optionally, rewrite the path to fit whatever service we're proxying to.

    url = "http://%s%s" % ("my_server.lol:9200", path)

    requestor = getattr(requests,request.method.lower())

    proxied_response = requestor(url, data=request.REQUEST, files=request.FILES)
    response = HttpResponse(proxied_response.content, content_type=proxied_response.headers.get('content-type'))
    for header_key, header_value in proxied_response.headers.iteritems():
    response[header_key] = header_value
    return response