HTTP/Get query params

#1

Hi there!
I want to send Get request of the following type:
https://some-url.com/?parameter=VALUE
And I also want to send some headers. Right now this looks like:
let res = http.get(endpoint +"?parameter=VALUE", { headers: headers });

Is there any way to send parameter not attached to url, but as some part of params object?

#2

There isn’t anything like that included in the k6 libs, but you can pretty easily write a query builder in JS like this:

import http from "k6/http";

function buildQuery(data) {
   let result = [];
   Object.keys(data).forEach((key) => {
       result.push(encodeURIComponent(key) + "=" + encodeURIComponent(data[key]));
   });
   return result.join("&");
}

export default function () {
   let urlQuery = buildQuery({ queryKey1: "someData=", queryKey2: "1254@@&%!" });
   console.log(urlQuery);
   let resp = http.put(`https://httpbin.org/anything?${urlQuery}`, { bodyKey1: "bval1", bodyKey2: "bval2" });
   console.log(resp.body);
}
1 Like