Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def _request(self, method, url, *,
raise RuntimeError('Session is closed')

redirects = 0
history = []
if not isinstance(method, upstr):
method = upstr(method)

Expand Down Expand Up @@ -193,6 +194,7 @@ def _request(self, method, url, *,
# redirects
if resp.status in (301, 302, 303, 307) and allow_redirects:
redirects += 1
history.append(resp)
if max_redirects and redirects >= max_redirects:
resp.close(force=True)
break
Expand Down Expand Up @@ -221,6 +223,7 @@ def _request(self, method, url, *,

break

resp.history = history
return resp

def ws_connect(self, url, *,
Expand Down
1 change: 1 addition & 0 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ class ClientResponse:
cookies = None # Response cookies (Set-Cookie)
content = None # Payload stream
headers = None # Response headers, CIMultiDictProxy
history = None # List of responses, if redirects occured

_connection = None # current connection
flow_control_class = FlowControlStreamReader # reader flow control
Expand Down
16 changes: 16 additions & 0 deletions docs/client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,22 @@ If a response contains some Cookies, you can quickly access them::
<aiohttp-client-session>` object.


Response History
----------------

If a request was redirected, it is possible to view previous responses using
the history attribute::

>>> r = await aiohttp.get('http://example.com/some/redirect/')
>>> r
<ClientResponse(http://example.com/some/other/url/) [200]>
>>> r.history
[<ClientResponse(http://example.com/some/redirect/) [301]>]

If no redirects occured or ``allow_redirects`` is set to ``False``, history will
be an empty list.


Timeouts
--------

Expand Down
5 changes: 5 additions & 0 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,11 @@ Response object

HTTP headers of response, :class:`CIMultiDictProxy`.

.. attribute:: history

List of :class:`ClientResponse` objects of preceding requests, if there
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:class:list`` is even better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of "List" in "List of :class:ClientResponse objects …"?

Or where exactly do you mean?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean

:class:`list` of :class:`ClientResponse` objects of preceding requests ...

were redirects.

.. method:: close()

Close response and underlying connection.
Expand Down