Loading Files
Use theopen() function to read file contents during the init context (before VU execution):
Text Files
Load and parse JSON data:Binary Files
Add"b" as the second argument to open binary files:
The
open() function executes in the init context, so files are loaded once per VU before the test starts. This is more efficient than reading files in every iteration.Basic File Upload
Upload a file using multipart form data:Image Upload Example
Upload an image with metadata:Advanced Multipart Uploads
For more control over multipart requests, use theFormData polyfill:
Multiple File Upload
When using
FormData, you must manually set the Content-Type header with the boundary value.Why Use FormData?
TheFormData polyfill solves two limitations:
- Ordered fields: JavaScript objects don’t guarantee property order, but some APIs (like AWS S3) require specific field ordering
- Multiple files per field: Upload multiple files under the same form field name
- Basic Upload
- FormData Upload
Simple uploads with automatic multipart handling:
Upload with Authentication
Combine file uploads with authentication:Dynamic File Generation
Generate file content dynamically:Large File Uploads
Test large file uploads with progress tracking:Common Scenarios
Avatar/Profile Picture Upload
Avatar/Profile Picture Upload
Document Upload with Metadata
Document Upload with Metadata
Batch File Upload
Batch File Upload
Best Practices
Load Files Once
Load files in init context, not in the VU function:Set Appropriate Timeouts
Large uploads may take time:Validate Upload Results
Always check upload success:API Reference
Key Functions
open(filePath, [mode])- Load file contents. Use"b"for binary modehttp.file(data, [filename], [contentType])- Wrap data asFileDatafor multipart requestsFormData.append(name, value)- Add fields to multipart form
Related Resources
HTTP Authentication
Add authentication to upload requests
API CRUD Operations
Complete API testing workflows
API Reference: http.file()
Complete http.file() documentation
API Reference: open()
Complete open() documentation