Skip to content

Commit 0a0789c

Browse files
committed
v30/request - streaming application/octet-stream requests
1 parent 178461a commit 0a0789c

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

aiopenapi3/v30/glue.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import io
12
from typing import List, Union, cast
23
import json
34
import urllib.parse
@@ -327,7 +328,14 @@ def _prepare_body(self, data, rbq):
327328
self.req.content = msg
328329
elif (ct := "application/octet-stream") in self.operation.requestBody.content:
329330
self.req.headers["Content-Type"] = ct
330-
self.req.content = data[1].read()
331+
value = data
332+
if isinstance(data, tuple) and len(data) >= 2:
333+
# (name, file-like-object, …)
334+
value = data[1]
335+
if isinstance(value, (io.IOBase, str, bytes)):
336+
self.req.content = value
337+
else:
338+
raise TypeError(data)
331339
else:
332340
raise NotImplementedError(self.operation.requestBody.content)
333341

docs/source/advanced.rst

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,19 +188,24 @@ File uploads via "multipart/form-data" as mentioned in the httpx documentation
188188
`encoding <https://www.python-httpx.org/advanced/#multipart-file-encoding>`_)
189189
do not require the content of the request to be in memory but work with file-like-objects instead.
190190

191-
httpx request streaming using file-like objects is limited to "multipart/form-data".
192-
It can not be used with "application/json" or "application/octet-stream".
191+
httpx request streaming using file-like objects is limited to "multipart/form-data" and "application/octet-stream".
193192
Additionally it does not support choice of encoding (such as base16, base64url or quoted-printable) as possible with OpenAPI v3.1 contentEncoding, which should not be a limitation.
193+
It can not be used with "application/json".
194+
194195

195196
Use via `Manual Requests`_ using the :meth:`~aiopenapi3.request.RequestBase.request` API.
196197

198+
multipart/form-data
199+
^^^^^^^^^^^^^^^^^^^
200+
201+
Pass the form fields as a list of tuples.
202+
197203
.. code:: python
198204
199205
data = [
200206
("name",('form-data:name', file-like-object, content_type, headers))
201207
]
202208
203-
A Request like
204209
205210
.. code::
206211
@@ -238,6 +243,18 @@ Mixing file-like-objects and other form data fields is possible.
238243
See :aioai3:ref:`tests.stream_test.test_request`.
239244
240245
246+
application/octet-stream
247+
^^^^^^^^^^^^^^^^^^^^^^^^
248+
249+
Pass the data as file-like-object or tuple where the second entry is a file-like-object as with multipart/form-data.
250+
251+
.. code:: python
252+
253+
data = Path("/data/file").open("rb")
254+
255+
data = ("name", Path("/data/file").open("rb"))
256+
257+
241258
Response Streaming
242259
------------------
243260

0 commit comments

Comments
 (0)