Responses

The core elements of an HTTP response are the status_code, headers and body.

httpx
>>> resp = httpx.Response(200, headers={'Content-Type': 'text/plain'}, content=b'hello, world')
>>> resp
<Response [200 OK]>
>>> resp.status_code
200
>>> resp.headers
<Headers {'Content-Type': 'text/html'}>
>>> resp.body()
b'hello, world'

Working with the response headers

The following headers have automatic behavior with Response instances...

Working with content types

Including HTML content...

httpx
>>> content = httpx.HTML('<html><head>...</head><body>...</body></html>')
>>> response = httpx.Response(200, content=content)

Including plain text content...

httpx
>>> content = httpx.Text('hello, world')
>>> response = httpx.Response(200, content=content)

Including JSON data...

httpx
>>> content = httpx.JSON({'message': 'hello, world'})
>>> response = httpx.Response(200, content=content)

Including content from a file...

httpx
>>> content = httpx.File('index.html')
>>> with httpx.Response(200, content=content) as response:
...     pass

Accessing response content

...

httpx
>>> content = response.body()

...

httpx
>>> text = response.text()
...

...

httpx
>>> data = response.json()

Requests URLs