Parameterize URL

Hi Team,

How to parameterize the request URL.

let resp= http.get('https://www.mysite.com/?s=${ransrch.p_Search}', searchHeader);

While running the test it getting failed. I can see the correct value on the console log, but the request getting failed.

 console.log('Search value : ',ransrch.p_Search);

image

Request

Hi @Gerard.

I’ve created a custom function for such purpose - maybe you can get some use of it.

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


export function securedGetWithParams(url, params, payload) {

    let searchParams = new URLSearchParams();

    for (const [key, value] of Object.entries(params)) {
        searchParams.append(key, value);
    }

    return http.get(`${url}?${searchParams.toString()}`, payload);
}

In case you need more info navigate to this link.

1 Like

Hi @rdt.one , your approach works well. But I achieved it by simply constructing the URL and using it.

let searchValue=ransrch.p_Search;
let url='You searched for ' searchValue; - myhost.com
let searchRes = http.get(url, searchHeader);

@Gerard you are using string in your origianl example while you seem to want to use templates literals. To do that you need to use a `(backtick) instead of '(single quote).

1 Like