You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/streams.md
+11-8Lines changed: 11 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,23 +10,22 @@ The base `Stream` class. The core of the interface is a subset of Python's `io.I
10
10
11
11
*`.read(size=-1)` - *(bytes)* Return the bytes from the data stream. If the `size` argument is omitted or negative then the entire stream will be read. If `size` is an positive integer then the call returns at most `size` bytes. A return value of `b''` indicates the end of the stream has been reached.
12
12
*`.close()` - Close the stream. Any further operations will raise a `ValueError`.
13
-
*`.closed` - *(bool)* Return a boolean indicating if the stream is closed or not.
14
13
15
-
Additionally, the following are also defined...
14
+
Additionally, the following properties are also defined...
16
15
17
-
*`.rewind()` - Rewind the current position to the start of the stream. Some implementations may raise a `ValueError`. For example file streams are rewindable, while http streams are not.
18
-
*`.size` - *(int or None)* Return an integer indicating the size of the stream, or `None` if the size is unknown. When working with HTTP/1.1 streams with a known size will include a `Content-Length: <size>` header, while unsized streams will include a `Content-Encoding: chunked` header and use chunked transfer framing.
16
+
*`.size` - *(int or None)* Return an integer indicating the size of the stream, or `None` if the size is unknown. When working with HTTP this is used to either set a `Content-Length: <size>` header, or a `Content-Encoding: chunked` header.
17
+
*`.content_type` - *(str or None)* Return a string indicating the content type of the data, or `None` if the content type is unknown. When working with HTTP this is used to optionally set a `Content-Type` header.
19
18
20
19
The `Stream` interface and `ContentType` interface are closely related, with streams being used as the abstraction for the bytewise representation, and content types being used to encapsulate the parsed data structure.
21
20
22
21
For example, encoding some `JSON` data...
23
22
24
23
```python
25
24
>>> data = httpx.JSON({'name': 'zelda', 'score': '478'})
26
-
>>> stream, ct_header= data.encode()
25
+
>>> stream = data.encode()
27
26
>>> stream.read()
28
27
b'{"name":"zelda","score":"478"}'
29
-
>>>ct_header
28
+
>>>stream.content_type
30
29
'application/json'
31
30
```
32
31
@@ -46,8 +45,10 @@ b'{"msg": "Hello, world!"}'
46
45
47
46
A byte stream returning content from a file.
48
47
48
+
The standard pattern for instantiating a `FileStream` is to use `File` as a context manager:
49
+
49
50
```python
50
-
>>>with httpx.FileStream.open('upload.json') as s:
51
+
>>>with httpx.File('upload.json') as s:
51
52
... s.read()
52
53
b'{"msg": "Hello, world!"}'
53
54
```
@@ -56,9 +57,11 @@ b'{"msg": "Hello, world!"}'
56
57
57
58
A byte stream returning multipart upload data.
58
59
60
+
The standard pattern for instantiating a `MultiPartStream` is to use `MultiPart` as a context manager:
0 commit comments