k6

A modern load testing tool, using Go and JavaScript

"like unit testing, for performance"

k6 is a modern load testing tool, building on Load Impact's years of experience. It provides a clean, approachable JavaScript scripting API, distributed and cloud execution, and orchestration via a REST API.

Get Started    Guides

Multipart requests (file uploads)

Uploading files is a common use case for many applications, so a tool like k6 needs to support testing such functionality.

The open() function

There is a builtin function, open(), that given a file or a URL will return its contents.

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

{
  "my_key": "has a value"
}
const data = JSON.parse(open("./data.json"));
export default function() {
  console.log(data.my_key);
}

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

const binFile = open("./image.png", "b");
export default function() {
  ...
}

Creating a 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):

import http from "k6/http";
import { sleep } from "k6";

let binFile = open("/path/to/file.bin", "b");

export default function() {
  var data = {
    field: "this is a standard form field",
    file: http.file(binFile, "test.bin")
  };
  var res = http.post("https://example.com/upload", data);
  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 http.post(), 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.

Multipart requests (file uploads)


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.