the total elapsed seconds.
* `def .raise_for_status()` - **None**
* `def .json()` - **Any**
-* `def .read()` - **bytes**
+* `def .aread()` - **bytes**
* `def .aiter_raw()` - **async bytes iterator**
* `def .aiter_bytes()` - **async bytes iterator**
* `def .aiter_text()` - **async text iterator**
* `def .aiter_lines()` - **async text iterator**
-* `def .close()` - **None**
+* `def .aclose()` - **None**
* `def .next()` - **Response**
## `Request`
* `.aiter_text()` - Instead of `response.iter_content(decode_unicode=True)`
* `.aiter_lines()` - Instead of `response.iter_lines()`
* `.aiter_raw()` - Use this instead of `response.raw`
-* `.read()` - Read the entire response body, making `request.text` and `response.content` available.
+* `.aread()` - Read the entire response body, making `request.text` and `response.content` available.
## SSL configuration
```
>>> async with httpx.stream("GET", "https://www.example.com") as r:
... if r.headers['Content-Length'] < TOO_LONG:
-... await r.read()
+... await r.aread()
... print(r.text)
```
if not stream:
try:
- await response.read()
+ await response.aread()
finally:
- await response.close()
+ await response.aclose()
return response
if not response.is_redirect:
return response
- await response.read()
+ await response.aread()
request = self.build_redirect_request(request, response)
history = history + [response]
except StopIteration:
return response
except BaseException as exc:
- await response.close()
+ await response.aclose()
raise exc from None
else:
request = next_request
- await response.close()
+ await response.aclose()
async def send_single_request(
self, request: Request, timeout: Timeout,
exc_value: BaseException = None,
traceback: TracebackType = None,
) -> None:
- await self.response.close()
+ await self.response.aclose()
if self.close_client:
await self.client.close()
f"response={proxy_response!r}"
)
if not (200 <= proxy_response.status_code <= 299):
- await proxy_response.read()
+ await proxy_response.aread()
raise ProxyError(
f"Non-2XX response received from HTTP proxy "
f"({proxy_response.status_code})",
def __repr__(self) -> str:
return f"<Response [{self.status_code} {self.reason_phrase}]>"
- async def read(self) -> bytes:
+ async def aread(self) -> bytes:
"""
Read and return the response content.
"""
self.is_stream_consumed = True
async for part in self._raw_stream:
yield part
- await self.close()
+ await self.aclose()
async def next(self) -> "Response":
"""
assert self.call_next is not None
return await self.call_next()
- async def close(self) -> None:
+ async def aclose(self) -> None:
"""
Close the response and release the connection.
Automatically called if the response body is read to completion.
async def test_stream_response(server):
async with httpx.Client() as client:
async with client.stream("GET", server.url) as response:
- body = await response.read()
+ body = await response.aread()
assert response.status_code == 200
assert body == b"Hello, world!"
async def test_stream_response(server):
async with httpx.Client() as client:
async with client.stream("GET", server.url) as response:
- content = await response.read()
+ content = await response.aread()
assert response.status_code == 200
assert content == b"Hello, world!"
"""
async with ConnectionPool() as http:
response = await http.request("GET", server.url)
- await response.read()
+ await response.aread()
assert len(http.active_connections) == 0
assert len(http.keepalive_connections) == 1
response = await http.request("GET", server.url)
- await response.read()
+ await response.aread()
assert len(http.active_connections) == 0
assert len(http.keepalive_connections) == 1
"""
async with ConnectionPool() as http:
response = await http.request("GET", server.url)
- await response.read()
+ await response.aread()
assert len(http.active_connections) == 0
assert len(http.keepalive_connections) == 1
http.KEEP_ALIVE_EXPIRY = 0.0
response = await http.request("GET", server.url)
- await response.read()
+ await response.aread()
assert len(http.active_connections) == 0
assert len(http.keepalive_connections) == 1
"""
async with ConnectionPool() as http:
response = await http.request("GET", server.url)
- await response.read()
+ await response.aread()
assert len(http.active_connections) == 0
assert len(http.keepalive_connections) == 1
response = await http.request("GET", "http://localhost:8000/")
- await response.read()
+ await response.aread()
assert len(http.active_connections) == 0
assert len(http.keepalive_connections) == 2
async with ConnectionPool(pool_limits=pool_limits) as http:
response = await http.request("GET", server.url)
- await response.read()
+ await response.aread()
assert len(http.active_connections) == 0
assert len(http.keepalive_connections) == 1
response = await http.request("GET", "http://localhost:8000/")
- await response.read()
+ await response.aread()
assert len(http.active_connections) == 0
assert len(http.keepalive_connections) == 1
assert len(http.active_connections) == 1
assert len(http.keepalive_connections) == 0
- await response.read()
+ await response.aread()
assert len(http.active_connections) == 0
assert len(http.keepalive_connections) == 1
assert len(http.active_connections) == 2
assert len(http.keepalive_connections) == 0
- await response_b.read()
+ await response_b.aread()
assert len(http.active_connections) == 1
assert len(http.keepalive_connections) == 1
- await response_a.read()
+ await response_a.aread()
assert len(http.active_connections) == 0
assert len(http.keepalive_connections) == 2
headers = [(b"connection", b"close")]
async with ConnectionPool() as http:
response = await http.request("GET", server.url, headers=headers)
- await response.read()
+ await response.aread()
assert len(http.active_connections) == 0
assert len(http.keepalive_connections) == 0
"""
async with ConnectionPool() as http:
response = await http.request("GET", server.url)
- await response.read()
- await response.close()
+ await response.aread()
+ await response.aclose()
assert len(http.active_connections) == 0
assert len(http.keepalive_connections) == 1
"""
async with ConnectionPool() as http:
response = await http.request("GET", server.url)
- await response.close()
+ await response.aclose()
assert len(http.active_connections) == 0
assert len(http.keepalive_connections) == 0
"""
async with ConnectionPool() as http:
response = await http.request("GET", server.url)
- await response.read()
+ await response.aread()
# Shutdown the server to close the keep-alive connection
await restart(server)
response = await http.request("GET", server.url)
- await response.read()
+ await response.aread()
assert len(http.active_connections) == 0
assert len(http.keepalive_connections) == 1
"""
async with ConnectionPool() as http:
response = await http.request("GET", server.url)
- await response.read()
+ await response.aread()
# Shutdown the server to close the keep-alive connection
await restart(server)
response = await http.request("GET", server.url)
- await response.read()
+ await response.aread()
assert len(http.active_connections) == 0
assert len(http.keepalive_connections) == 1
"""
async with ConnectionPool(pool_limits=httpx.PoolLimits(hard_limit=1)) as http:
response = await http.request("GET", server.url)
- await response.read()
+ await response.aread()
# Close the connection so we're forced to recycle it
await restart(server)
async def test_get(server):
async with HTTPConnection(origin=server.url) as conn:
response = await conn.request("GET", server.url)
- await response.read()
+ await response.aread()
assert response.status_code == 200
assert response.content == b"Hello, world!"
response = await conn.request(
"GET", server.url.copy_with(path="/premature_close")
)
- await response.read()
+ await response.aread()
@pytest.mark.usefixtures("async_environment")
"""
async with HTTPConnection(origin=https_server.url, verify=ca_cert_pem_file) as conn:
response = await conn.request("GET", https_server.url)
- await response.read()
+ await response.aread()
assert response.status_code == 200
assert response.content == b"Hello, world!"
assert resp.request.url == "https://example.com"
assert resp.request.headers["Host"] == "example.com"
- await resp.read()
+ await resp.aread()
# Make another request to see that the tunnel is re-used.
resp = await proxy.request("GET", "https://example.com/target")
assert resp.request.url == "https://example.com/target"
assert resp.request.headers["Host"] == "example.com"
- await resp.read()
+ await resp.aread()
recv = raw_io.received_data
assert len(recv) == 5
assert response.encoding == "ascii"
assert response.is_closed
- content = await response.read()
+ content = await response.aread()
assert content == b"Hello, world!"
assert response.content == b"Hello, world!"
async def test_text_interface():
response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)
- await response.read()
+ await response.aread()
content = ""
async for part in response.aiter_text():
async def test_lines_interface():
response = httpx.Response(200, content=b"Hello,\nworld!", request=REQUEST)
- await response.read()
+ await response.aread()
content = []
async for line in response.aiter_lines():
async def test_stream_interface_after_read():
response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)
- await response.read()
+ await response.aread()
content = b""
async for part in response.aiter_bytes():
assert response.status_code == 200
assert not response.is_closed
- content = await response.read()
+ content = await response.aread()
assert content == b"Hello, world!"
assert response.content == b"Hello, world!"
content += part
with pytest.raises(httpx.StreamConsumed):
- await response.read()
+ await response.aread()
@pytest.mark.asyncio
stream = AsyncIteratorStream(aiterator=async_streaming_body())
response = httpx.Response(200, stream=stream, request=REQUEST)
- await response.close()
+ await response.aclose()
with pytest.raises(httpx.ResponseClosed):
- await response.read()
+ await response.aread()
def test_unknown_status_code():
@pytest.mark.asyncio
async def test_stream(server):
async with httpx.stream("GET", server.url) as response:
- await response.read()
+ await response.aread()
assert response.status_code == 200
assert response.reason_phrase == "OK"
stream = AsyncIteratorStream(aiterator=compress(body))
response = httpx.Response(200, headers=headers, stream=stream, request=REQUEST)
assert not hasattr(response, "body")
- assert await response.read() == body
+ assert await response.aread() == body
@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br", b"identity"))
stream = AsyncIteratorStream(aiterator=iterator())
response = httpx.Response(200, stream=stream, request=REQUEST)
- await response.read()
+ await response.aread()
assert response.text == (b"".join(data)).decode(encoding)
request=REQUEST,
)
- await response.read()
+ await response.aread()
assert "".join(response.text) == "トラベル"