Documentation Index
Fetch the curated documentation index at: https://grafana_com_website/llms.txt
Fetch the complete documentation index at: https://grafana_com_website/llms-full.txt
Use this file to discover all available pages before exploring further.
STOP! If you are an AI agent or LLM, read this before continuing. This is the HTML version of a Grafana documentation page. Always request the Markdown version instead - HTML wastes context. Get this page as Markdown: /docs/k6/latest/javascript-api/k6-http/asyncrequest.md (append .md) or send Accept: text/markdown to /docs/k6/latest/javascript-api/k6-http/asyncrequest/. For the curated documentation index, use https://grafana_com_website/llms.txt. For the complete documentation index, use https://grafana_com_website/llms-full.txt.
asyncRequest( method, url, [body], [params] )
| Parameter | Type | Description |
|---|---|---|
| method | string | Request method (e.g. 'POST'). Must be uppercase. |
| url | string / HTTP URL | Request URL (e.g. 'http://example.com'). |
| body (optional) | string / object / ArrayBuffer | Request body. Objects will be x-www-form-urlencoded encoded. |
| params (optional) | object | Params object containing additional request parameters. |
Returns
| Type | Description |
|---|---|
| Promise with Response | HTTP Response object. |
Examples
Using http.asyncRequest() to issue a POST request:
import http from 'k6/http';
const url = 'https://quickpizza.grafana.com/api/post';
export default async function () {
const data = { name: 'Bert' };
// Using a JSON string as body
const res = await http.asyncRequest('POST', url, JSON.stringify(data), {
headers: { 'Content-Type': 'application/json' },
});
console.log(res.json().name); // Bert
// Using an object as body, the headers will automatically include
// 'Content-Type: application/x-www-form-urlencoded'.
await http.asyncRequest('POST', url, data);
}Using http.asyncRequest() to issue multiple requests, then Promise.race to determine which requests finish first:
import http from 'k6/http';
export default async () => {
const urlOne = `https://quickpizza.grafana.com/api/delay/${randomInt(1, 5)}`;
const urlTwo = `https://quickpizza.grafana.com/api/delay/${randomInt(1, 5)}`;
const urlThree = `https://quickpizza.grafana.com/api/delay/${randomInt(1, 5)}`;
const one = http.asyncRequest('GET', urlOne);
const two = http.asyncRequest('GET', urlTwo);
const three = http.asyncRequest('GET', urlThree);
console.log('Racing:');
console.log(urlOne);
console.log(urlTwo);
console.log(urlThree);
const res = await Promise.race([one, two, three]);
console.log('winner is', res.url, 'with duration of', res.timings.duration + 'ms');
};
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}Note
http.asyncRequesthas no current way to abort a request.In the preceding script, after
resgets the value from the fastest request, the other requests will continue to execute. This might block the end of the iteration, because the iteration only stops once all async jobs finish.
Was this page helpful?
Related resources from Grafana Labs

