diff --git a/http-web-services.html b/http-web-services.html index c9e1baf..3b8b2b5 100755 --- a/http-web-services.html +++ b/http-web-services.html @@ -578,7 +578,7 @@ reply: 'HTTP/1.1 200 OK'
 # continued from the previous example
->>> print(dict(response.items()))                                     
+>>> response                                     
 {'status': '200',
  'content-length': '3070',
  'content-location': 'http://diveintopython3.org/examples/feed.xml',  
@@ -597,7 +597,35 @@ reply: 'HTTP/1.1 200 OK'
  • The response you get back from this single call to the request() method is the response from the final URL.
  • httplib2 adds the final URL to the response dictionary, as content-location. This is not a header that came from the server; it’s specific to httplib2.
  • Apropos of nothing, this feed is compressed. -
  • And cacheable. (This is important, as you’ll see in the next example.) +
  • And cacheable. (This is important, as you’ll see in a minute.) + + +

    The response you get back gives you information about the final URL. What if you want more information about the intermediate URLs, the ones that eventually redirected to the final URL? httplib2 lets you do that, too. + +

    +# continued from the previous example
    +>>> response.previous           
    +{'status': '302',
    + 'content-length': '228',
    + 'content-location':
    + 'http://diveintopython3.org/examples/feed-302.xml',
    + 'expires': 'Thu, 16 Jul 2009 20:15:58 GMT',
    + 'server': 'Apache',
    + 'connection': 'close',
    + 'location': 'http://diveintopython3.org/examples/feed.xml',
    + 'cache-control': 'max-age=86400',
    + 'date': 'Wed, 15 Jul 2009 20:15:58 GMT',
    + 'content-type': 'text/html; charset=iso-8859-1'}
    +>>> type(response)              
    +<class 'httplib2.Response'>
    +>>> type(response.previous)
    +<class 'httplib2.Response'>
    +>>> response.previous.previous  
    +>>>
    +
      +
    1. The response.previous attribute holds a reference to the previous response object that httplib2 followed to get to the current response object. +
    2. Both response and response.previous are httplib2.Response objects. +
    3. That means you can check response.previous.previous to follow the redirect chain backwards even further. (Scenario: one URL redirects to a second URL which redirects to a third URL. It could happen!) In this case, we’ve already reached the beginning of the redirect chain, so the attribute is None.

    What happens if you request the same URL again?