Overview
Some APIs need to accept raw data as the entire body of a request, such as binary file uploads, streaming data, or custom binary formats. Express Zod API provides theez.raw() schema for this purpose.
Using ez.raw()
Use the proprietaryez.raw() schema to accept raw binary data:
Configuration
Raw data is parsed using therawParser configuration option, which defaults to express.raw():
The Raw Buffer
Raw data is available as aBuffer in the input.raw property:
Complete Raw Data Example
With Route Parameters
Combine raw data with path parameters:Processing Different Data Types
Binary File Upload
Text Data
JSON with Custom Processing
Protocol Buffers / MessagePack
Validation and Security
Size Validation
Content Type Validation
Magic Byte Validation
Streaming Large Files
For very large files, consider streaming instead of buffering:Client Examples
Using Fetch
Using ReadStream
Use Cases
Binary File Uploads
Binary File Uploads
Accept entire files as raw binary data without multipart encoding overhead.
Custom Binary Protocols
Custom Binary Protocols
Implement APIs that accept Protocol Buffers, MessagePack, or other binary formats.
Streaming Data
Streaming Data
Accept streaming data for real-time processing or large file uploads.
Webhook Payloads
Webhook Payloads
Process raw webhook data before parsing to verify signatures.
Comparison with File Uploads
| Feature | ez.raw() | ez.upload() |
|---|---|---|
| Content Type | application/octet-stream | multipart/form-data |
| Multiple files | No | Yes |
| Additional fields | Limited (params/query) | Yes (form fields) |
| Metadata | Manual | Automatic (filename, mimetype) |
| Use case | Binary protocols, streaming | Traditional file uploads |
Best Practices
Set Size Limits
Set Size Limits
Always configure appropriate size limits in
rawParser to prevent memory exhaustion.Validate Content Type
Validate Content Type
Check the Content-Type header to ensure you’re receiving the expected data format.
Use Magic Bytes
Use Magic Bytes
For security, validate file types using magic bytes rather than trusting extensions or MIME types.
Consider Memory Usage
Consider Memory Usage
For large files, consider streaming approaches to avoid loading entire files into memory.
Related Topics
- File Uploads - Using multipart/form-data
- Non-JSON Responses - Serving binary data
- Input Sources - Configuring input sources