cURL to k6

Input
Output

cURL to k6

Copy a request as cURL from your browser's DevTools, paste the response it gave, and get a k6 script that sends the same request and checks what comes back. It's set to one user and one run to start; raise vus and iterations, or add stages, when you load test.

Example: a POST that creates a task

A cURL command with a JSON body, and the 201 response. The script sends the same body and checks each field, with the UUID by type:

The response

{
  "id": "6f1c2a9e-3b7d-4c55-9a10-2e8f4b7c9d31",
  "title": "Ship order ORD-20419",
  "done": false,
  "tags": [
    "shipping",
    "priority"
  ]
}

The request (cURL)

curl -X POST https://api.example.com/v1/tasks -H 'Content-Type: application/json' -d '{"title":"Ship order ORD-20419","tags":["shipping","priority"]}'

Output

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

export const options = { vus: 1, iterations: 1 };

export default function () {
  const res = http.request("POST", "https://api.example.com/v1/tasks", JSON.stringify({"title":"Ship order ORD-20419","tags":["shipping","priority"]}), { headers: { "Content-Type": "application/json" } });
  check(res, {
    "status is 201": (r) => r.status === 201,
    "id is a string": (r) => typeof r.json().id === "string",
    "title is \"Ship order ORD-20419\"": (r) => r.json().title === "Ship order ORD-20419",
    "done is false": (r) => r.json().done === false,
    "tags has 2 items": (r) => r.json().tags.length === 2,
    "tags[0] is \"shipping\"": (r) => r.json().tags[0] === "shipping",
  });
}

How do I use the cURL to k6?

  1. Paste the response JSON.
  2. Paste the cURL command in the second box.
  3. Copy the script and run it with k6 run script.js.

What does the cURL to k6 do?

  • Method, headers and body from the cURL command
  • A named check per field, shown in k6's summary
  • options with one user to start from

When to use the cURL to k6

k6 tests an API by running a script many times at once, and a load test only means something if the responses are right. Checks catch the API that stays fast by answering with errors. This builds the script from a request you already have and the response it should give.

The script uses http.get for GET requests and http.request for the others, with the cURL command's headers and JSON body. Each check has a readable name, like items has 2 items, so k6's summary shows which ones failed and how often. options starts at one virtual user and one iteration; change it to options = { vus: 50, duration: "1m" } for a load test.

Frequently asked questions

Are the checks failures in k6?
No, a failed check is counted but doesn't stop the run. To fail the test, add a threshold: thresholds: { checks: ["rate>0.99"] } in options.
What about timestamps and ids in the response?
They're checked by type, not value, so the checks still pass when each call returns a new id or time.
Is my data uploaded?
No. Bracely runs entirely in your browser, so nothing you paste or open leaves your device.

More JSON tools

Bracely has 104 JSON tools in one page. Related ones:

Runs entirely in your browser. Nothing you paste is uploaded. Made by Saurabh Pagrut · About · Privacy policy
Last updated · Version 6.1 · Changelog
Saurabh Pagrut on GitHub · LinkedIn · X · YouTube · Instagram