Home / Function/ iter_content() — requests Function Reference

iter_content() — requests Function Reference

Architecture documentation for the iter_content() function in models.py from the requests codebase.

Function python CoreAPI VerbHandlers calls 2 called by 3

Entity Profile

Dependency Diagram

graph TD
  1d84ce86_28dc_c5f4_e8af_24874e68169e["iter_content()"]
  eb32847e_3797_d01a_6e44_345e9ea7e251["Response"]
  1d84ce86_28dc_c5f4_e8af_24874e68169e -->|defined in| eb32847e_3797_d01a_6e44_345e9ea7e251
  89ebbd87_2a88_90dc_1402_eb6ff1e8f6fc["__iter__()"]
  89ebbd87_2a88_90dc_1402_eb6ff1e8f6fc -->|calls| 1d84ce86_28dc_c5f4_e8af_24874e68169e
  6ee47ade_e2ec_47c7_716f_07feb21308f7["iter_lines()"]
  6ee47ade_e2ec_47c7_716f_07feb21308f7 -->|calls| 1d84ce86_28dc_c5f4_e8af_24874e68169e
  1c1a1a8e_6f9f_4aff_15d5_66872a75ed6a["content()"]
  1c1a1a8e_6f9f_4aff_15d5_66872a75ed6a -->|calls| 1d84ce86_28dc_c5f4_e8af_24874e68169e
  e2fc9cdb_4232_d5c7_7411_2e239e62c7f9["iter_slices()"]
  1d84ce86_28dc_c5f4_e8af_24874e68169e -->|calls| e2fc9cdb_4232_d5c7_7411_2e239e62c7f9
  7791240e_4e36_1e16_7d34_5ca6bb94eae7["stream_decode_response_unicode()"]
  1d84ce86_28dc_c5f4_e8af_24874e68169e -->|calls| 7791240e_4e36_1e16_7d34_5ca6bb94eae7
  style 1d84ce86_28dc_c5f4_e8af_24874e68169e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/requests/models.py lines 801–857

    def iter_content(self, 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.
        """

        def generate():
            # Special case for urllib3.
            if hasattr(self.raw, "stream"):
                try:
                    yield from self.raw.stream(chunk_size, decode_content=True)
                except ProtocolError as e:
                    raise ChunkedEncodingError(e)
                except DecodeError as e:
                    raise ContentDecodingError(e)
                except ReadTimeoutError as e:
                    raise ConnectionError(e)
                except SSLError as e:
                    raise RequestsSSLError(e)
            else:
                # Standard file-like object.
                while True:
                    chunk = self.raw.read(chunk_size)
                    if not chunk:
                        break
                    yield chunk

            self._content_consumed = True

        if self._content_consumed and isinstance(self._content, bool):
            raise StreamConsumedError()
        elif chunk_size is not None and not isinstance(chunk_size, int):
            raise TypeError(
                f"chunk_size must be an int, it is instead a {type(chunk_size)}."
            )
        # simulate reading small chunks of the content
        reused_chunks = iter_slices(self._content, chunk_size)

        stream_chunks = generate()

        chunks = reused_chunks if self._content_consumed else stream_chunks

        if decode_unicode:
            chunks = stream_decode_response_unicode(chunks, self)

        return chunks

Domain

Subdomains

Frequently Asked Questions

What does iter_content() do?
iter_content() is a function in the requests codebase, defined in src/requests/models.py.
Where is iter_content() defined?
iter_content() is defined in src/requests/models.py at line 801.
What does iter_content() call?
iter_content() calls 2 function(s): iter_slices, stream_decode_response_unicode.
What calls iter_content()?
iter_content() is called by 3 function(s): __iter__, content, iter_lines.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free