Skip to main content
k6 provides full support for HTTP/1.1 and HTTP/2 protocols, enabling you to test REST APIs, web services, and HTTP endpoints with a rich set of methods and configuration options.

HTTP Methods

k6 supports all standard HTTP methods through the k6/http module.
import http from 'k6/http';

export default function () {
  http.get('https://quickpizza.grafana.com');
}

HTTP/2 Support

k6 automatically handles HTTP/2 connections when the server supports it. You can verify the protocol version in your tests.
import http from "k6/http";
import { check } from "k6";

export default function () {
  check(http.get("https://quickpizza.grafana.com"), {
    "status is 200": (r) => r.status == 200,
    "protocol is HTTP/2": (r) => r.proto == "HTTP/2.0",
  });
}

Authentication

k6 supports HTTP Basic Authentication through URL credentials or custom headers.
import encoding from "k6/encoding";
import http from "k6/http";
import { check } from "k6";

export default function() {
  // Method 1: Username and password in URL
  let res = http.get("http://user:[email protected]/basic-auth/user/passwd");

  check(res, {
    "status is 200": (r) => r.status === 200,
    "is authenticated": (r) => r.json().authenticated === true,
    "is correct user": (r) => r.json().user === "user"
  });

  // Method 2: Authorization header
  res = http.get(
    "http://httpbin.org/basic-auth/user/passwd",
    {
      headers: {
        "Authorization": "Basic " + encoding.b64encode("user:passwd")
      }
    }
  );

  check(res, {
    "status is 200": (r) => r.status === 200,
    "is authenticated": (r) => r.json().authenticated === true,
  });
}

Batch Requests

Perform multiple HTTP requests in parallel for improved performance.
import { check } from 'k6';
import http from 'k6/http';

export default function() {
  const responses = http.batch([
    "https://quickpizza.grafana.com/test.k6.io",
    "https://quickpizza.grafana.com/pi.php"
  ]);

  check(responses[0], {
    "main page 200": res => res.status === 200,
  });

  check(responses[1], {
    "pi page 200": res => res.status === 200,
    "pi page has right content": res => res.body === "3.14",
  });
}
Batch requests are executed in parallel and wait for all requests to complete before continuing.

Request Parameters

Configure HTTP requests with various parameters:
import http from "k6/http";

export default function() {
  const params = {
    headers: {
      "Content-Type": "application/json",
      "X-Custom-Header": "value",
    },
    tags: {
      name: "MyRequest",
    },
    timeout: "60s",
    redirects: 5,
    cookies: {
      session: "session-id-value",
    },
  };

  http.post(
    "https://api.example.com/endpoint",
    JSON.stringify({ key: "value" }),
    params
  );
}

Response Object

HTTP responses provide comprehensive information about the request:
PropertyDescription
statusHTTP status code
status_textHTTP status text
protoProtocol version (e.g., “HTTP/1.1”, “HTTP/2.0”)
headersResponse headers as object
bodyResponse body as string
json()Parse response body as JSON
cookiesResponse cookies
timingsRequest timing information
errorError message (if request failed)
error_codeError code (if request failed)

TLS Configuration

k6 provides constants for configuring TLS connections:
import http from "k6/http";

export const options = {
  tlsVersion: {
    min: http.TLS_1_2,
    max: http.TLS_1_3,
  },
};

export default function() {
  http.get("https://api.example.com");
}

Available TLS Constants

  • http.TLS_1_0
  • http.TLS_1_1
  • http.TLS_1_2
  • http.TLS_1_3
Manage cookies across requests with cookie jars:
import http from "k6/http";

export default function() {
  // Cookies are automatically maintained across requests
  const jar = http.cookieJar();
  
  jar.set("https://example.com", "session", "value");
  
  http.get("https://example.com/authenticated");
}

Async Requests

k6 supports asynchronous HTTP requests using promises:
Async requests are an experimental feature and may not be suitable for all use cases.
import http from "k6/http";

export default async function() {
  const response = await http.asyncRequest(
    "GET",
    "https://api.example.com/data"
  );
  console.log(response.status);
}

Best Practices

  1. Use batch requests for parallel operations to improve test efficiency
  2. Set appropriate timeouts to prevent hanging requests
  3. Verify protocol versions when testing HTTP/2 support
  4. Use tags to organize and filter metrics
  5. Check response status before parsing JSON to avoid errors
  6. Reuse connections by making multiple requests in the same VU iteration

Build docs developers (and LLMs) love