简体   繁体   中英

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?

None.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM