Response Headers quickstart
authorKenneth Reitz <me@kennethreitz.com>
Mon, 15 Aug 2011 02:30:30 +0000 (22:30 -0400)
committerKenneth Reitz <me@kennethreitz.com>
Mon, 15 Aug 2011 02:30:30 +0000 (22:30 -0400)
docs/user/quickstart.rst

index 06d5f28662b656b62a816bb5d64d57c92366985e..c28ad1c60127cc97114ba37f066ab5bc02fed023 100644 (file)
@@ -69,4 +69,39 @@ But, sice our ``status_code`` was ``200``, when we call it::
     >>> r.raise_for_status()
     None
 
-All is well.
\ No newline at end of file
+All is well.
+
+
+Response Headers
+----------------
+
+We can view the server's response headers with a simple Python dictionary
+interface::
+
+    >>> r.headers
+    {
+        'status': '200 OK',
+        'content-encoding': 'gzip',
+        'transfer-encoding': 'chunked',
+        'connection': 'close',
+        'server': 'nginx/1.0.4',
+        'x-runtime': '148ms',
+        'etag': '"e1ca502697e5c9317743dc078f67693f"',
+        'content-type': 'application/json; charset=utf-8'
+    }
+
+The dictionary is special, though: it's made just for HTTP headers. According to `RFC 2616 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html>`_, HTTP
+Headers are case-insensitive.
+
+So, we can access the headers using any capitalization we want::
+
+    >>> r.headers['Content-Type']
+    'application/json; charset=utf-8'
+
+    >>> r.headers.get('content-type')
+    'application/json; charset=utf-8'
+
+If a header doesn't exist in the Response, its value defaults to ``None``::
+
+    >>> r.headers['X-Random']
+    None
\ No newline at end of file