Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For non-streaming cases, populate request.content automatically. #1583

Merged
merged 3 commits into from
Apr 19, 2021

Conversation

tomchristie
Copy link
Member

@tomchristie tomchristie commented Apr 19, 2021

Minor user-experience & consistency tweak.

For non-streaming cases we should pre-load request.content.

This leads to neater consistency between requests/responses, and ensures that the request content is readily accessible to introspection.

>>> r = httpx.Request("POST", "https://www.example.com", content=b'...')
>>> r.content  # Previously this would raise 'httpx.RequestNotRead'
b'...'
>>> r = httpx.Request("POST", "https://www.example.com", json={"example": "payload"})
>>> r.content  # Previously this would raise 'httpx.RequestNotRead'
b'{"example": "payload"}'
>>> r = httpx.Request("POST", "https://www.example.com", content=[b'.' for i in range(3)])
>>> r.content
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_models.py", line 865, in content
    raise RequestNotRead()
httpx.RequestNotRead: Attempted to access streaming request content, without having called `read()`.

Or, eg...

Previously...

>>> import httpx
>>> r = httpx.get("https://www.example.com")
>>> r.request.content
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_models.py", line 862, in content
    raise RequestNotRead()
httpx.RequestNotRead: Attempted to access request content, without having called `read()`  # Eh?

Now...

>>> import httpx
>>> r = httpx.get("https://www.example.com")
>>> r.request.content
b''

@tomchristie tomchristie added the user-experience Ensuring that users have a good experience using the library label Apr 19, 2021
@tomchristie
Copy link
Member Author

Going to push on with this sans-review, because it'll otherwise block other bits of work that I'd like to push on with here.

@tomchristie tomchristie merged commit 966550b into master Apr 19, 2021
@tomchristie tomchristie deleted the populate-request-content-for-non-streaming-cases branch April 19, 2021 10:13
@lundberg
Copy link
Contributor

Much nicer 👍

Not sure, but maybe the httpx.RequestNotRead and httpx.ResponseNotRead should be renamed to align with #1584, e.g. turn both into a httpx.StreamNotRead?

@tomchristie
Copy link
Member Author

Interesting yup. Hierarchy feels nicer that way for sure.

@tomchristie tomchristie mentioned this pull request Apr 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
user-experience Ensuring that users have a good experience using the library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants