Skip to content

Response Object ## Business Purpose

Provide a rich object representing the server’s response, enabling convenient access to content, headers, encoding, cookies, and computed properties like redirect history. ## Current Behaviors - Response.__init__ sets default attributes including _content as False and _content_consumed as False src/requests/models.py:763. - Response.content property reads raw body into bytes and marks content as consumed src/requests/models.py:1031. - Response.text property decodes content using detected encoding with fallback to UTF-8 src/requests/models.py:1031. - Response.json() decodes JSON using detected or provided encoding src/requests/models.py:1031. - Response.raise_for_status() raises HTTPError for 4xx/5xx status codes src/requests/models.py:1140. - Response.history is a list of responses that handled the redirect chain src/requests/models.py:860. - Response.elapsed is a timedelta measured with preferred_clock() src/requests/sessions.py:752. ## Technical Implementation The Response class is defined in src/requests/models.py. It uses SessionRedirectMixin via the session to build redirect history. Content access triggers lazy reading from the underlying raw stream. ## Definition of Done - r.content returns bytes for any response. - r.text returns a string, even for responses with unknown encoding. - r.json() returns a dict for valid JSON responses and raises requests.exceptions.JSONDecodeError for malformed JSON. - r.raise_for_status() raises HTTPError for status 404 and 500. - r.history is a list (not tuple) for redirected requests.