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
/// admonition | Significantly improved in BlackSheep 2.6.0
357
+
type: info
358
+
359
+
BlackSheep 2.6.0 introduces major improvements for handling `multipart/form-data` requests with the `FromForm` and `FromFiles` binders:
360
+
361
+
-**Memory-efficient file handling**: Files use `SpooledTemporaryFile` - small files (<1MB) stay in memory, larger files automatically spill to temporary disk files
362
+
-**True streaming parsing**: New streaming API for processing multipart data without buffering (`async for part in request.multipart_stream()`)
363
+
-**Automatic resource cleanup**: The framework automatically cleans up file resources at the end of each request
364
+
-**Better file API**: `FileBuffer` class provides clean methods (`read()`, `seek()`, `close()`, `save_to()`) for working with uploaded files
365
+
-**OpenAPI documentation**: `FromText` and `FromFiles` are now fully documented in OpenAPI schemas
366
+
367
+
///
368
+
369
+
### FromForm: Form URL encoded and Multipart
370
+
371
+
The `FromForm` binder handles both `application/x-www-form-urlencoded` and `multipart/form-data` content types:
372
+
373
+
```python
374
+
from dataclasses import dataclass
375
+
from blacksheep import FromForm, post
376
+
377
+
378
+
@dataclass
379
+
classUserInput:
380
+
name: str
381
+
email: str
382
+
age: int
383
+
384
+
385
+
@post("/users")
386
+
asyncdefcreate_user(input: FromForm[UserInput]):
387
+
user =input.value
388
+
# user.name, user.email, user.age are automatically parsed
389
+
return {"message": f"User {user.name} created"}
390
+
```
391
+
392
+
### FromFiles: File Upload Handling
393
+
394
+
The `FromFiles` binder provides efficient handling of file uploads with memory-efficient buffering:
395
+
396
+
```python
397
+
from blacksheep import FromFiles, post
398
+
399
+
400
+
@post("/upload")
401
+
asyncdefupload_files(files: FromFiles):
402
+
# files.value is a list of FormPart objects
403
+
for file_part in files.value:
404
+
# Access file metadata
405
+
file_name = file_part.file_name.decode() if file_part.file_name else"unknown"
406
+
content_type = file_part.content_type.decode() if file_part.content_type elseNone
407
+
408
+
# file_part.file is a FormPart instance with efficient memory handling
409
+
# Small files (<1MB) are kept in memory, larger files use temporary disk files
410
+
file_buffer = file_part.file
411
+
412
+
# Or save directly to disk (recommended for large files)
For handling very large file uploads efficiently, use the streaming API directly:
450
+
451
+
```python
452
+
from blacksheep import post, Request, created
453
+
454
+
455
+
@post("/upload-large")
456
+
asyncdefupload_large_files(request: Request):
457
+
# Stream multipart data without buffering
458
+
asyncfor part in request.multipart_stream():
459
+
if part.file_name:
460
+
# This is a file upload
461
+
file_name = part.file_name.decode()
462
+
463
+
# Stream the file content in chunks
464
+
withopen(f"./uploads/{file_name}", "wb") as f:
465
+
asyncfor chunk in part.stream():
466
+
f.write(chunk)
467
+
else:
468
+
# This is a regular form field
469
+
field_name = part.name.decode() if part.name else""
470
+
field_value = part.data.decode()
471
+
472
+
return created()
473
+
```
474
+
475
+
### Automatic Resource Cleanup
476
+
477
+
BlackSheep automatically manages file resources. The framework calls `Request.dispose()` at the end of each request-response cycle to clean up temporary files created during multipart parsing. This ensures:
478
+
479
+
- Temporary files are automatically deleted
480
+
- Memory is properly released
481
+
- No manual cleanup is required in most cases
482
+
483
+
If you need manual control over resource cleanup (e.g., in long-running background tasks), you can call `request.dispose()` explicitly.
484
+
353
485
## Defining a custom binder
354
486
355
487
To define a custom binder, define a `BoundValue[T]` class and a `Binder`
Copy file name to clipboardExpand all lines: blacksheep/docs/openapi.md
+124Lines changed: 124 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -222,6 +222,130 @@ components: {}
222
222
tags: []
223
223
```
224
224
225
+
### Request body binders support
226
+
227
+
/// admonition | Enhanced in BlackSheep 2.6.0
228
+
type: info
229
+
230
+
BlackSheep 2.6.0 adds full OpenAPI documentation support for `FromText` and `FromFiles` binders. These binders are now automatically documented with appropriate request body schemas and content types.
231
+
232
+
///
233
+
234
+
BlackSheep automatically generates OpenAPI documentation for various request body binders. The following examples assume the `docs` handler has been set up as described in the [Built-in support](#built-in-support-for-openapi-documentation) section.
235
+
236
+
#### FromJSON
237
+
238
+
Documented with `application/json` content type and the appropriate schema:
0 commit comments