Menu
Documentationbreadcrumb arrow Grafana k6breadcrumb arrow Examplesbreadcrumb arrow URLs with query parameters
Open source

URLs with query parameters

How to use URL and URLSearchParams imported from jslib.k6.io to construct URLs with/without query parameters.

URL

JavaScript
import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js';
import http from 'k6/http';

export default function () {
  const url = new URL('https://k6.io');

  url.searchParams.append('utm_medium', 'organic');
  url.searchParams.append('utm_source', 'test');
  url.searchParams.append('multiple', ['foo', 'bar']);

  const res = http.get(url.toString());
  // https://k6.io?utm_medium=organic&utm_source=test&multiple=foo%2Cbar
}
NameTypeDescription
URLSearchParams(init)Constructorinit Optional: One of [USVString, sequence of pairs or record ]
toString()MethodReturns a USVString containing the whole URL
hashPropertyA USVString containing a ‘#’ followed by the fragment identifier of the URL.
hostPropertyA USVString containing the host, that is the hostname, and then, if the port of the URL is nonempty, a ‘:’, followed by the port of the URL.
hostnamePropertyA USVString containing the domain name of the URL.
hrefPropertyReturns a USVString containing the whole URL.
originPropertyReturns a USVString containing the origin of the URL, that is its scheme, its domain and its port.
passwordPropertyA USVString containing the password specified before the domain name.
pathnamePropertyIs a USVString containing an initial ‘/’ followed by the path of the URL, not including the query string or fragment.
portPropertyA USVString containing the port number of the URL.
protocolPropertyA USVString containing the protocol scheme of the URL, including the final ‘:’.
searchPropertyA USVString indicating the URL’s parameter string; if any parameters are provided, this string includes all of them, beginning with the leading ? character.
searchParamsPropertyA URLSearchParams object which can be used to access the individual query parameters found in search.
usernamePropertyA USVString containing the username specified before the domain name.

URLSearchParams

JavaScript
import { URLSearchParams } from 'https://jslib.k6.io/url/1.0.0/index.js';
import http from 'k6/http';

export default function () {
  const searchParams = new URLSearchParams([
    ['utm_medium', 'organic'],
    ['utm_source', 'test'],
    ['multiple', 'foo'],
    ['multiple', 'bar'],
  ]);

  const res = http.get(`${'https://k6.io'}?${searchParams.toString()}`);
  // https://k6.io?utm_medium=organic&utm_source=test&multiple=foo&multiple=bar
}
NameTypeDescription
URLSearchParams(init)Constructorinit Optional: One of [USVString, sequence of pairs or record ]
toString()MethodReturns a query string (questionmark is omitted).
append()MethodAppends a specified key/value pair as a new search parameter.
delete()MethodDeletes the given search parameter, and its associated value, from the list of all search parameters.
entries()MethodReturns an iterator allowing iteration through all key/value pairs contained in this object.
forEach()MethodAllows iteration through all values contained in this object via a callback function. callback(value, key).
get()MethodReturns the first value associated with the given search parameter.
getAll()MethodReturns all the values associated with a given search parameter.
has()MethodReturns a Boolean that indicates whether a parameter with the specified name exists.
keys()MethodReturns an iterator allowing iteration through all keys of the key/value pairs contained in this object.
values()MethodReturns an iterator allowing iteration through all values of the key/value pairs contained in this object.
set()MethodSets the value associated with a given search parameter to the given value. If there were several matching values, this method deletes the others. If the search parameter doesn’t exist, this method creates it.
sort()MethodSorts all key/value pairs, if any, by their keys.