No results for

Powered byAlgolia

Data Uploads

suggest edits

Example to execute a load test that will upload a file to the System Under Test(SUT).

The open() function

Using the built-in function, open(), we are able to read the contents of a file given a filename or URL.

Below is a simple example showing how to load the contents of a local file data.json.

2 "my_key": "has a value"
Loading a local JSON file using open()
1const data = JSON.parse(open('./data.json'));
3export default function () {
4 console.log(data.my_key);

If you want to open a binary file you need to pass in "b" as the second argument.

Loading a binary file using open()
1const binFile = open("./image.png", "b");
3export default function() {
4 ...

Multipart request (uploading a file)

Now that you know how to load a local file, let's look at a script that creates a POST request to upload this data to an API endpoint along with a regular text field (field in the example below):

POST upload example
1import http from 'k6/http';
2import { sleep } from 'k6';
4let binFile = open('/path/to/file.bin', 'b');
6export default function () {
7 var data = {
8 field: 'this is a standard form field',
9 file: http.file(binFile, 'test.bin'),
10 };
12 var res ='', data);
13 sleep(3);

In the example above we use the http.file() API to wrap the file contents in a FileData object. When passing a JS object as the body parameter to, or any of the other HTTP request functions, where one of the property values is a FileData a multipart request will be constructed and sent.

Relevant k6 APIs