- class requests_cache.models.response.CachedResponse(content=None, next=None, cookies=NOTHING, created_at=NOTHING, elapsed=NOTHING, encoding=None, expires=None, headers=NOTHING, history=NOTHING, raw=NOTHING, reason=None, request=NOTHING, status_code=0, url=None)¶
A class that emulates
requests.Response, with some additional optimizations for serialization.
- property apparent_encoding¶
The apparent encoding, provided by the charset_normalizer or chardet libraries.
- cache_key = None¶
Releases the connection back to the pool. Once this method has been called the underlying
rawobject must not be accessed again.
Note: Should not normally need to be called explicitly.
- property content¶
Content of the response, in bytes.
A CookieJar of Cookies the server sent back.
The amount of time elapsed between sending the request and the arrival of the response (as a timedelta). This property specifically measures the time taken between sending the first byte of the request and finishing parsing the headers. It is therefore unaffected by consuming the response content or the value of the
Encoding to decode with when accessing r.text.
- classmethod from_response(original_response, expires=None, **kwargs)¶
Create a CachedResponse based on an original Response or another CachedResponse object
Case-insensitive Dictionary of Response Headers. For example,
headers['content-encoding']will return the value of a
A list of
Responseobjects from the history of the Request. Any redirect responses will end up here. The list is sorted from the oldest to the most recent request.
- property is_permanent_redirect¶
True if this Response one of the permanent versions of redirect.
- property is_redirect¶
True if this Response is a well-formed HTTP redirect that could have been processed automatically (by
- iter_content(chunk_size=1, decode_unicode=False)¶
Iterates over the response data. When stream=True is set on the request, this avoids reading the content at once into memory for large responses. The chunk size is the number of bytes it should read into memory. This is not necessarily the length of each item returned as decoding can take place.
chunk_size must be of type int or None. A value of None will function differently depending on the value of stream. stream=True will read data as it arrives in whatever size the chunks are received. If stream=False, data is returned as a single chunk.
If decode_unicode is True, content will be decoded using the best available encoding based on the response.
- iter_lines(chunk_size=512, decode_unicode=False, delimiter=None)¶
Iterates over the response data, one line at a time. When stream=True is set on the request, this avoids reading the content at once into memory for large responses.
This method is not reentrant safe.
Returns the json-encoded content of a response, if any.
**kwargs – Optional arguments that
simplejson.JSONDecodeError – If the response body does not contain valid json and simplejson is installed.
json.JSONDecodeError – If the response body does not contain valid json and simplejson is not installed on Python 3.
ValueError – If the response body does not contain valid json and simplejson is not installed on Python 2.
Returns the parsed header links of the response, if any.
- property next¶
Returns a PreparedRequest for the next request in a redirect chain, if there is one.
- property ok¶
Returns True if
status_codeis less than 400, False if not.
This attribute checks if the status code of the response is between 400 and 600 to see if there was a client error or a server error. If the status code is between 200 and 400, this will return True. This is not a check to see if the response code is
HTTPError, if one occurred.
File-like object representation of response (for advanced usage). Use of
stream=Truebe set on the request. This requirement does not apply for use internally to Requests.
Textual reason of responded HTTP Status, e.g. “Not Found” or “OK”.
PreparedRequestobject to which this is a response.
Set a new expiration for this response, and determine if it is now expired
Integer Code of responded HTTP Status, e.g. 404 or 200.
- property text¶
Content of the response, in unicode.
If Response.encoding is None, encoding will be guessed using
The encoding of the response content is determined based solely on HTTP headers, following RFC 2616 to the letter. If you can take advantage of non-HTTP knowledge to make a better guess at the encoding, you should set
r.encodingappropriately before accessing this property.
Final URL location of Response.
Get a formatted datetime string in the local time zone
Convert a file size in bytes into a human-readable format
- requests_cache.models.response.set_response_defaults(response, cache_key=None)¶
Set some default CachedResponse values on a requests.Response object, so they can be expected to always be present