Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Now, let's get started:
<Response [200 OK]>
>>> r.status_code
200
>>> r.headers['content-type']
>>> r.content_type
'text/html; charset=UTF-8'
>>> r.text
'<!doctype html>\n<html>\n<head>\n<title>Example Domain</title>...'
Expand Down
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
* `.content` - **bytes**
* `.text` - **str**
* `.encoding` - **str**
* `.content_type` - **Optional[str]**
* `.is_redirect` - **bool**
* `.request` - **Request**
* `.next_request` - **Optional[Request]**
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Now, let's get started:
<Response [200 OK]>
>>> r.status_code
200
>>> r.headers['content-type']
>>> r.content_type
'text/html; charset=UTF-8'
>>> r.text
'<!doctype html>\n<html>\n<head>\n<title>Example Domain</title>...'
Expand Down
2 changes: 1 addition & 1 deletion httpx/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def print_help() -> None:


def get_lexer_for_response(response: Response) -> str:
content_type = response.headers.get("Content-Type")
content_type = response.content_type
if content_type is not None:
mime_type, _, _ = content_type.partition(";")
try:
Expand Down
13 changes: 10 additions & 3 deletions httpx/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,11 +690,10 @@ def charset_encoding(self) -> str | None:
"""
Return the encoding, as specified by the Content-Type header.
"""
content_type = self.headers.get("Content-Type")
if content_type is None:
if self.content_type is None:
return None

return _parse_content_type_charset(content_type)
return _parse_content_type_charset(self.content_type)

def _get_content_decoder(self) -> ContentDecoder:
"""
Expand Down Expand Up @@ -1075,6 +1074,14 @@ async def aclose(self) -> None:
with request_context(request=self._request):
await self.stream.aclose()

@property
def content_type(self) -> str | None:
"""
Return the Content-Type header.
"""
result = self.headers.get("Content-Type")
return str(result) if result else None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the str() cast? Isn't it str already?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not str, it's Any

class Headers(typing.MutableMapping[str, str]):
    def get(self, key: str, default: typing.Any = None) -> typing.Any:
        ...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, result defaults to None if the key is not present in the HTTP headers due to how the Headers.get function works, so you may as well return self.headers.get("Content-Type").



class Cookies(typing.MutableMapping[str, str]):
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ def test_ensure_ascii_false_with_french_characters():
assert (
"ça va" in response.text
), "ensure_ascii=False should preserve French accented characters"
assert response.headers["Content-Type"] == "application/json"
assert response.content_type == "application/json"


def test_separators_for_compact_json():
Expand All @@ -501,7 +501,7 @@ def test_separators_for_compact_json():
assert (
response.text == '{"clé":"valeur","liste":[1,2,3]}'
), "separators=(',', ':') should produce a compact representation"
assert response.headers["Content-Type"] == "application/json"
assert response.content_type == "application/json"


def test_allow_nan_false():
Expand Down