Enabling file streaming operations
Support for streaming is critical for applications that need to send or receive large amounts of data between client and server without first buffering the data into memory, potentially exhausting this system resource.
Streaming download
Creating an endpoint with a top-level binary response body allows treating that response as a streamable, and iterating over it without loading the entire response into memory. This is useful for large file downloads, long-running streaming responses, and more.
In an OpenAPI document, this can be modeled as a binary stream. Here’s an example of a get operation with content type as application/octet-stream.
/streamable:
get:
operationId: streamable
responses:
"200":
description: OK
content:
application/octet-stream:
schema:
title: bytes
type: string
format: binarynextra-code
Streaming uploads
Streaming is useful when uploading large files. Certain SDK methods will be generated that accept files as part of a multipart request. It is possible (and recommended) to upload files as a stream rather than reading the entire contents into memory. This avoids excessive memory consumption and potentially crashing with out-of-memory errors when working with large files.
In this example, a request to upload a file is managed as a multipart/form-data request.
/file:
post:
summary: Upload file
operationId: upload
requestBody:
content:
multipart/form-data:
schema:
$ref: "#/components/schemas/UploadFileRequest"
required: true
responses:
"200":
description: ""
headers:
Action-Id:
required: true
schema:
type: string
content:
application/json:
schema:
$ref: "#/components/schemas/File"nextra-code
Last updated on