Skip to content

Instantly share code, notes, and snippets.

@sdnts
Last active January 10, 2023 20:50
Show Gist options
  • Save sdnts/b57985b0649d3407a7aa9de1bd327990 to your computer and use it in GitHub Desktop.
Save sdnts/b57985b0649d3407a7aa9de1bd327990 to your computer and use it in GitHub Desktop.
Postman pm.sendRequest example

To send a request via the sandbox, you can use pm.sendRequest.

pm.test("Status code is 200", function () {
    pm.sendRequest('https://postman-echo.com/get', 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');
    });
});

Without additional options, this will sent a GET request to the URL specified. If you prefer to be more explicit, you can use the complete syntax:

pm.sendRequest({
    url: 'https://postman-echo.com/post',
    method: 'POST',
    header: 'headername1:value1',
    body: {
        mode: 'raw',
        raw: JSON.stringify({ key: "this is json" })
    }
}, function (err, res) {
    console.log(res);
});
@abeward
Copy link

abeward commented Jan 7, 2022

Is there any way to run a saved request from the collection in the Pre-request Script?

Not that I'm aware of. I've recently asked them for this feature, though. It would be a huge timesaver if we could just pm.sendRequest by GUID.

@brendan-robert
Copy link

any update on this? @ABlomberg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment