How to send a request with pm.sendRequest script in Tests using variables in the URL?

Hello. I use this script to send the next request instead of creating a new request in the collection. I need to use variables in that URL, but I’m facing this issue, could anyone help me?

pm.test("Status code is 200", function () {
    pm.sendRequest('{{url}}/albums/picture/63862864732648', function (err, res) {
        pm.expect(err).to.not.be.ok;
        pm.expect(res).to.have.property('code', 200);
        pm.expect(res).to.have.property('status', 'OK');
    });
});

Assuming you are using a csv/json file, in your prerequest script you can do:

pm.request.url = pm.iterationData.get('<name of column with your url>');

This will set the url for the request.
If you also need to set the method, you can do the same thing

pm.request.method = pm.iterationData.get('<name of column with the http method>');

It does not work :frowning:

You would need to reference the variable like this or using the specific variable scope:

pm.test("Status code is 200", function () {
    pm.sendRequest(`${pm.variables.get('url')}/albums/picture/63862864732648`, function (err, res) {
        pm.expect(err).to.not.be.ok;
        pm.expect(res).to.have.property('code', 200);
        pm.expect(res).to.have.property('status', 'OK');
    });
});
1 Like

Thanks :green_heart: it works

1 Like