Response

class impit.Response(status_code, *, content=None, headers=None, default_encoding=None, url=None)

Response object returned by impit clients (Client or AsyncClient instances).

When constructed manually (e.g. for testing purposes), the following parameters can be provided:

Parameters:
  • status_code (int) – HTTP status code of the response (e.g., 200, 404)

  • content (bytes) – Response body as bytes (or None for empty body)

  • headers (dict[str, str]) – Response headers as a dictionary (or None for empty headers)

  • default_encoding (str | None) – Default encoding for the response text. Used only if content-type header is not present or does not specify a charset.

  • url (str) – Final URL of the response.

async aclose()

Asynchronously close the response and release resources.

Note

This method is for internal use only.

Use the async with statement to ensure proper resource management when working with asynchronous clients.

async with impit.stream('GET', get_httpbin_url('/')) as response:
    assert response.status_code == 200

assert response.is_closed is True
Return type:

None

aiter_bytes()

Asynchronously iterate over the response content in chunks. Asynchronous version of iter_bytes().

Useful for streaming large responses without loading the entire content into memory.

async with AsyncClient() as client:
    async with client.stream("GET", "https://example.com/largefile") as response:
        async for chunk in response.aiter_bytes():
            process(chunk)  # Process each chunk as it is received
Return type:

AsyncIterator[bytes]

async aread()

Asynchronously read the response content as bytes. Asynchronous version of read().

Useful for consuming the entire response body in one go (not chunked).

async with AsyncClient() as client:
    async with client.stream("GET", "https://example.com/largefile") as response:
        content = await response.aread()
        process(content)  # Process the entire content at once
Return type:

bytes

close()

Close the response and release resources.

Warning

You should not need to call this method directly.

Use the with statement to ensure proper resource management when working with synchronous clients.

with impit.stream('GET', get_httpbin_url('/')) as response:
    assert response.status_code == 200

assert response.is_closed is True
Return type:

None

content: bytes

Contains the response body as bytes. If the response was created with stream=True, this will be empty until the content is read using read() or iter_bytes().

response = await client.get("https://crawlee.dev")
print(response.content) # b'<!DOCTYPE html>...'
encoding: str

Response content encoding. Determined from content-type header or by bytestream prescan. Falls back to ‘utf-8’ if not found.

response = await client.get("https://crawlee.dev")
print(response.encoding) # 'utf-8'

This can be used to decode the Response body manually. By default, text uses this encoding to decode content.

headers: dict[str, str]

Response headers as a Python dictionary.

response = await client.get("https://crawlee.dev")
print(response.headers) # {'content-type': 'text/html; charset=utf-8', ... }
http_version: str

HTTP version (e.g., ‘HTTP/1.1’, ‘HTTP/2’) negotiated for the response during the TLS handshake.

response = await client.get("https://crawlee.dev")
print(response.http_version) # 'HTTP/2'
is_closed: bool

True if the response has been closed using the close() method, False otherwise.

Closing a response releases any underlying resources (e.g., network connections).

response = await client.get("https://crawlee.dev")
print(response.is_closed) # False
response.close()
print(response.is_closed) # True
is_redirect: bool

True if the response is a redirect (has a 3xx status code), False otherwise.

response = await client.get("https://crawlee.dev")
print(response.is_redirect) # False
is_stream_consumed: bool

Whether the response stream has been consumed or closed.

If this is True, calling read() or iter_bytes() will raise a StreamConsumed or StreamClosed error.

The read response body is still available in the content attribute.

response = await client.get("https://crawlee.dev", stream=True)
print(response.is_stream_consumed) # False
for chunk in response.iter_bytes():
    pass
print(response.is_stream_consumed) # True
# calling response.read() or response.iter_bytes() again will raise StreamConsumed error
# read the content of the response using response.content
iter_bytes()

Iterate over the response content in chunks. Synchronous version of aiter_bytes().

Useful for streaming large responses without loading the entire content into memory. .. code-block:: python

with Client() as client:
with client.stream(“GET”, “https://example.com/largefile”) as response:
for chunk in response.iter_bytes():

process(chunk) # Process each chunk as it is received

Return type:

Iterator[bytes]

json()

Parse the response content as JSON.

Note

This method will raise a DecodingError if the response content is not valid JSON.

response = await client.get("https://api.example.com/data")
data = response.json()
print(data)  # Parsed JSON data as a Python object (dict, list, etc.)
Return type:

Any

read()

Read the response content as bytes. Synchronous version of aread().

Useful for consuming the entire response body in one go (not chunked).

with Client() as client:
    with client.stream("GET", "https://example.com/largefile") as response:
        content = response.read()
        process(content)  # Process the entire content at once
Return type:

bytes

reason_phrase: str

HTTP reason phrase for the response (e.g., ‘OK’, ‘Not Found’). This maps the numerical status_code to a human-readable string.

response = await client.get("https://crawlee.dev")
print(response.reason_phrase) # 'OK'
status_code: int

HTTP status code of the response (e.g., 200, 404).

Tip

If the status code indicates an error (4xx or 5xx), you can raise an HTTPStatusError exception using the raise_for_status() method.

response = await client.get("https://crawlee.dev")
print(response.status_code) # 200
text: str

Response body as text. Decoded from content using encoding.

response = await client.get("https://crawlee.dev")
print(response.text) # '<!DOCTYPE html>...'
url: str

The final URL of the response. This may differ from the requested URL if redirects were followed (see the follow_redirects parameter in Client and AsyncClient).

response = await client.get("https://crawlee.dev")
print(response.url) # 'https://crawlee.dev'