# Responses The core elements of an HTTP response are the `status_code`, `headers` and `body`.
httpx
```{ .python .httpx } >>> resp = httpx.Response(200, headers={'Content-Type': 'text/plain'}, content=b'hello, world') >>> resp >>> resp.status_code 200 >>> resp.headers >>> resp.body b'hello, world' ``` ```{ .python .ahttpx .hidden } >>> resp = ahttpx.Response(200, headers={'Content-Type': 'text/plain'}, content=b'hello, world') >>> resp >>> resp.status_code 200 >>> resp.headers >>> resp.body b'hello, world' ``` ## Working with the response headers The following headers have automatic behavior with `Response` instances... * `Content-Length` - Responses including a response body must always include either a `Content-Length` header or a `Transfer-Encoding: chunked` header. This header is automatically populated if `content` is not `None` and the content is a known size. * `Transfer-Encoding` - Responses automatically include a `Transfer-Encoding: chunked` header if `content` is not `None` and the content is an unkwown size. * `Content-Type` - Responses automatically include a `Content-Type` header if `content` is set using the [Content Type] API. ## Working with content types Including HTML content... ```{ .python .httpx } >>> content = httpx.HTML('......') >>> response = httpx.Response(200, content=content) ``` ```{ .python .ahttpx .hidden } >>> content = ahttpx.HTML('......') >>> response = ahttpx.Response(200, content=content) ``` Including plain text content... ```{ .python .httpx } >>> content = httpx.Text('hello, world') >>> response = httpx.Response(200, content=content) ``` ```{ .python .ahttpx .hidden } >>> content = ahttpx.Text('hello, world') >>> response = ahttpx.Response(200, content=content) ``` Including JSON data... ```{ .python .httpx } >>> content = httpx.JSON({'message': 'hello, world'}) >>> response = httpx.Response(200, content=content) ``` ```{ .python .ahttpx .hidden } >>> content = ahttpx.JSON({'message': 'hello, world'}) >>> response = ahttpx.Response(200, content=content) ``` Including content from a file... ```{ .python .httpx } >>> content = httpx.File('index.html') >>> with httpx.Response(200, content=content) as response: ... pass ``` ```{ .python .ahttpx .hidden } >>> content = ahttpx.File('index.html') >>> async with ahttpx.Response(200, content=content) as response: ... pass ``` ## Accessing response content ... ```{ .python .httpx } >>> response.body ``` ```{ .python .ahttpx .hidden } >>> response.body ``` ... ```{ .python .httpx } >>> response.text ... ``` ```{ .python .ahttpx .hidden } >>> response.text ... ``` --- ← [Requests](requests.md) [URLs](urls.md) →