Most modern APIs return JSON data. Extract values by parsing the response:
import http from 'k6/http';import { check } from 'k6';export default function () { // Make a request that returns JSON data const reqHeaders = { Authorization: 'Token abcdef0123456789', }; const res = http.get('https://quickpizza.grafana.com/api/doughs', { headers: reqHeaders, }); // Extract data from JSON by parsing and navigating the object const dough1 = res.json().doughs[0]; check(dough1, { 'dough 1 has correct name': (s) => s.name === 'Thin', 'dough 1 has correct ID': (s) => s.ID === 1, }); // Use the extracted data in subsequent requests const orderRes = http.post( 'https://quickpizza.grafana.com/api/orders', JSON.stringify({ doughId: dough1.ID }), { headers: reqHeaders } );}
Use res.json() to parse the entire response, or res.json('path.to.field') to extract a specific field directly.
For more control, extract specific fields manually:
import http from 'k6/http';import { sleep } from 'k6';export default function () { // Request the page containing a form const res = http.get('https://test.k6.io/my_messages.php', { responseType: 'text' }); // Query the HTML for an input field named "redir" const elem = res.html().find('input[name=redir]'); // Get the value of the attribute "value" const val = elem.attr('value'); // Use the extracted value in subsequent requests console.log('The value of the hidden field redir is: ' + val); // Now make a request with the extracted value http.post('https://test.k6.io/submit', { redir: val, message: 'Hello from k6!', }); sleep(1);}
If you set discardResponseBodies: true in options, you must override it per request with {responseType: "text"} to access the response body.
For responses that aren’t JSON or HTML, extract values using string boundaries:
import { findBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';import { check } from 'k6';import http from 'k6/http';export default function () { // This request returns XML const res = http.get('https://quickpizza.grafana.com/api/xml?color=green'); // Use findBetween to extract the first <value> tag const color = findBetween(res.body, '<value>', '</value>'); check(color, { 'color is correct': (t) => t === 'green', }); // Use extracted value in next request http.post('https://quickpizza.grafana.com/api/verify', JSON.stringify({ color: color }) );}
export function setup() { // Get data once for all VUs const res = http.get('https://api.example.com/config'); return { apiKey: res.json('apiKey'), endpoints: res.json('endpoints'), };}export default function (data) { // All VUs use shared data http.get(data.endpoints.users, { headers: { 'X-API-Key': data.apiKey }, });}
import http from 'k6/http';export default function () { const res = http.get('https://api.example.com/products'); // Get all product IDs const products = res.json('products'); const productIds = products.map(p => p.id); // Use random product ID const randomId = productIds[Math.floor(Math.random() * productIds.length)]; http.get(`https://api.example.com/products/${randomId}`);}