Raw Response

class requests_cache.models.raw_response.CachedHTTPResponse(*args, body=None, headers=None, **kwargs)[source]

Bases: urllib3.response.HTTPResponse

A serializable dataclass that emulates HTTPResponse. Supports streaming requests and generator usage.

The only action this doesn’t support is explicitly calling read() with decode_content=False.

CONTENT_DECODERS = ['gzip', 'deflate']
DECODER_ERROR_CLASSES = (<class 'OSError'>, <class 'zlib.error'>)
REDIRECT_STATUSES = [301, 302, 303, 307, 308]

Flush and close the IO object.

This method has no effect if the file is already closed.

property closed
property connection
property data

Read and discard any remaining HTTP response data in the response connection.

Unread data in the HTTPResponse connection blocks the connection from being released back to the pool.


Returns underlying file descriptor if one exists.

OSError is raised if the IO object does not use a file descriptor.


Flush write buffers, if applicable.

This is not implemented for read-only and non-blocking streams.

classmethod from_httplib(r, **response_kw)

Given an http.client.HTTPResponse instance r, return a corresponding urllib3.response.HTTPResponse object.

Remaining parameters are passed to the HTTPResponse constructor, along with original_response=r.

classmethod from_response(original_response)[source]

Create a CachedHTTPResponse based on an original response


original_response (Response) –


Should we redirect and where to?


Truthy redirect location string if we got a redirect status code and valid location. None if redirect status and no location. False if not a redirect status code.

getheader(name, default=None)

Returns the URL that was the source of this response. If the request that generated this response redirected, this method will return the final redirect location.

headers = None

Return whether this is an ‘interactive’ stream.

Return False if it can’t be determined.

read(amt=None, decode_content=None, **kwargs)[source]

Simplified reader for cached content that emulates urllib3.response.HTTPResponse.read()

read_chunked(amt=None, decode_content=None)

Similar to HTTPResponse.read(), but with an additional parameter: decode_content.

  • amt – How much of the content to read. If specified, caching is skipped because it doesn’t make sense to cache partial content as the full response.

  • decode_content – If True, will attempt to decode the body based on the ‘content-encoding’ header.


Return whether object was opened for reading.

If False, read() will raise OSError.

readline(size=- 1, /)

Read and return a line from the stream.

If size is specified, at most size bytes will be read.

The line terminator is always b’n’ for binary files; for text files, the newlines argument to open can be used to select the line terminator(s) recognized.

readlines(hint=- 1, /)

Return a list of lines from the stream.

hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds hint.


No-op for compatibility


Reset raw response file pointer, and optionally update content


body (Optional[bytes]) –


Change stream position.

Change the stream position to the given byte offset. The offset is interpreted relative to the position indicated by whence. Values for whence are:

  • 0 – start of stream (the default); offset should be zero or positive

  • 1 – current stream position; offset may be negative

  • 2 – end of stream; offset is usually negative

Return the new absolute position.


Return whether object supports random access.

If False, seek(), tell() and truncate() will raise OSError. This method may need to do a test seek().


body (bytes) –

stream(amt=None, **kwargs)[source]

Simplified generator over cached content that emulates urllib3.response.HTTPResponse.stream()


Checks if the underlying file-like object looks like a http.client.HTTPResponse object. We do this by testing for the fp attribute. If it is present we assume it returns raw chunks as processed by read_chunked().


Obtain the number of bytes pulled over the wire so far. May differ from the amount of content returned by :meth:urllib3.response.HTTPResponse.read if bytes are encoded on the wire (e.g, compressed).


Truncate file to size bytes.

File pointer is left unchanged. Size defaults to the current IO position as reported by tell(). Returns the new size.


Return whether object was opened for writing.

If False, write() will raise OSError.

writelines(lines, /)

Write a list of lines to stream.

Line separators are not added, so it is usual for each of the lines provided to have a line separator at the end.