Skip to content
Myr

Response Headers quickstart

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Kenneth Reitz · committed by Kenneth Reitz · parent f137db6 · Browse files · 1 file+36 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+36 added−1 removed

  • other:+36 added−1 removed

1 file+36 added−1 removed

modified:docs/user/quickstart.rst+36 added-1 removed
67 unmodified lines
6869 >>> r.raise_for_status()70 None7172All is well.72All is well.737475Response Headers76----------------7778We can view the server's response headers with a simple Python dictionary79interface::8081 >>> r.headers82 {83 'status': '200 OK',84 'content-encoding': 'gzip',85 'transfer-encoding': 'chunked',86 'connection': 'close',87 'server': 'nginx/1.0.4',88 'x-runtime': '148ms',89 'etag': '"e1ca502697e5c9317743dc078f67693f"',90 'content-type': 'application/json; charset=utf-8'91 }9293The dictionary is special, though: it's made just for HTTP headers. According to `RFC 2616 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html>`_, HTTP94Headers are case-insensitive.9596So, we can access the headers using any capitalization we want::9798 >>> r.headers['Content-Type']99 'application/json; charset=utf-8'100101 >>> r.headers.get('content-type')102 'application/json; charset=utf-8'103104If a header doesn't exist in the Response, its value defaults to ``None``::105106 >>> r.headers['X-Random']107 None