No results for

Powered byAlgolia

URLs with query parameters

suggest edits

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

URL

URL
1import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js';
2import http from 'k6/http';
3
4export default function () {
5 const url = new URL('https://k6.io');
6
7 url.searchParams.append('utm_medium', 'organic');
8 url.searchParams.append('utm_source', 'test');
9 url.searchParams.append('multiple', ['foo', 'bar']);
10
11 const res = http.get(url.toString());
12 // https://k6.io?utm_medium=organic&utm_source=test&multiple=foo%2Cbar
13}
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 domain (that is the hostname) followed by (if a port was specified) a ':' and the port of the URL.
hostnamePropertyA USVString containing the domain (that is the hostname) followed by (if a port was specified) a ':' and the port 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

URLSearchParams
1import { URLSearchParams } from 'https://jslib.k6.io/url/1.0.0/index.js';
2import http from 'k6/http';
3
4export default function () {
5 const searchParams = new URLSearchParams([
6 ['utm_medium', 'organic'],
7 ['utm_source', 'test'],
8 ['multiple', 'foo'],
9 ['multiple', 'bar'],
10 ]);
11
12 const res = http.get(`${'https://k6.io'}?${searchParams.toString()}`);
13 // https://k6.io?utm_medium=organic&utm_source=test&multiple=foo&multiple=bar
14}
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.