Home > Blockchain >  What's the order of response.history?
What's the order of response.history?

Time:01-15

Let's suppose I have sent an HTTP request to A which redirected to B then C.

response = requests.get(A_url, allow_redirects = True)

From what I understand the contents of response are A.

But what's the order inside response.history? is it [B,C] or [C,B]?

CodePudding user response:

From the requests source code, which uses hist.append(resp), it looks to be ordered "ascending" (sequentially) in the order seen. So, [B, C] from your example.

        hist = []  # keep track of history

        url = self.get_redirect_target(resp)
        previous_fragment = urlparse(req.url).fragment
        while url:
            prepared_request = req.copy()

            # Update history and keep track of redirects.
            # resp.history must ignore the original request in this loop
            hist.append(resp)
            resp.history = hist[1:]

            ...

This is a block of code from .resolve_redirects() which keeps looking for redirects until it is no longer redirected. . get_redirect_target(), in turn, while stop returning a URL (it will return None) if there is no redirect target, ending the while url loop seen above.

  •  Tags:  
  • Related