Tutorial :How to make a Django passthrough view?


I want to make a Django view that does the following:

  • Receive an HttpRequest on api/some/url/or/other
  • Passes this through to another server at some/url/or/other (rewrite the URL, basically)
    • Adding a cookie based on session data in Django
    • Using the same method, data, params, et al, that were in the original request
  • Returns verbatim the response to the API call
    • Must store the cookies that came back from the call in the session
    • Must include the Django session cookie in the returned HttpResponse

What tools already exist in Django to do this?



You'll have to code your own wrapper utility, using one of httplib / urllib / urllib2 libs to connect to the other server.
Most likely you will have to extract all the relevant info from the HttpRequest object and use that to manually construct your own request in said util function.

Regarding receiving the response from that other server, it will depend a little bit on wether you need that response only asynchronously or quasi-synchronously.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »