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
Streams provide a minimal file-like interface for reading bytes from a data source. They are used as the abstraction for reading the body of a request or response.
4
+
5
+
The interfaces here are simplified versions of Python's standard I/O operations.
6
+
7
+
## Stream
8
+
9
+
The base `Stream` class. The core of the interface is a subset of Python's `io.IOBase`...
10
+
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
+
*`.close()` - Close the stream. Any further operations will raise a `ValueError`.
13
+
14
+
Additionally, the following properties are also defined...
15
+
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.
18
+
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.
20
+
21
+
For example, encoding some `JSON` data...
22
+
23
+
```python
24
+
>>> data = httpx.JSON({'name': 'zelda', 'score': '478'})
25
+
>>> stream = data.encode()
26
+
>>> stream.read()
27
+
b'{"name":"zelda","score":"478"}'
28
+
>>> stream.content_type
29
+
'application/json'
30
+
```
31
+
32
+
---
33
+
34
+
## ByteStream
35
+
36
+
A byte stream returning fixed byte content. Similar to Python's `io.BytesIO` class.
37
+
38
+
```python
39
+
>>> s = httpx.ByteStream(b'{"msg": "Hello, world!"}')
40
+
>>> s.read()
41
+
b'{"msg": "Hello, world!"}'
42
+
```
43
+
44
+
## FileStream
45
+
46
+
A byte stream returning content from a file.
47
+
48
+
The standard pattern for instantiating a `FileStream` is to use `File` as a context manager:
49
+
50
+
```python
51
+
>>>with httpx.File('upload.json') as s:
52
+
... s.read()
53
+
b'{"msg": "Hello, world!"}'
54
+
```
55
+
56
+
## MultiPartStream
57
+
58
+
A byte stream returning multipart upload data.
59
+
60
+
The standard pattern for instantiating a `MultiPartStream` is to use `MultiPart` as a context manager:
61
+
62
+
```python
63
+
>>> files = {'avatar-upload': 'image.png'}
64
+
>>>with httpx.MultiPart(files=files) as s:
65
+
... s.read()
66
+
# ...
67
+
```
68
+
69
+
## HTTPStream
70
+
71
+
A byte stream returning unparsed content from an HTTP request or response.
0 commit comments