sendRequest and generating the query string


#1

Hi,

Is there a way to have sendRequest generate the query string for a GET request rather than manually appending it to the url parameter ?

for example this works fine:

const GETRequest = {
url: ‘https://api.com’ + ‘?’ + ‘abc=def’,
method: ‘GET’,
};
pm.sendRequest(
GETRequest,
function (err, res) {
pm.environment.set(“token”, res.json().token);
}
);

but I would like to do something like:

const GETRequest = {
url: ‘https://api.com’,
method: ‘GET’,
qs: {
‘abc’: ‘def’,
},
};
pm.sendRequest(
GETRequest,
function (err, res) {
pm.environment.set(“token”, res.json().token);
}
);


#2

No, but you could write your own builder/helper function to accomplish this for you:

function getQueryString (obj) {
    return Object.keys(obj).map((key) => `${key}=${obj[key]}`).join('&');
}

const qs = {
    'abc': 'def',
    'ghi': 'jkl'
};

pm.sendRequest({
   url: 'https://postman-echo.com/get?' + getQueryString(qs),
   method: 'GET'
}, (err, res) => {
    pm.test('Request parameters match', () => {
         pm.expect(JSON.stringify(res.json().args)).to.equal(JSON.stringify(qs));
    });
});

#3

spot on, many thanks for the prompt suggestion