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 this:
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?
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) {
const result = [];
Object.keys(data)
.forEach((key) => {
const encode = encodeURIComponent;
result.push(encode(key) + "=" + encode(data[key]));
});
return result.join("&");
}
export default function () {
let urlQuery = buildQuery({
queryKey1: "someData",
queryKey2: "1254@@&%!",
});
console.log(urlQuery);
const body = {
bodyKey1: "bval1",
bodyKey2: "bval2",
};
let resp = http.put(
`https://httpbin.org/anything?${urlQuery}`,
body
);
console.log(resp.body);
}
1 Like