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);
});
@jprealini
Copy link

jprealini commented Oct 6, 2021

@arun-a-nayagam I have seen stuff like that happen because the "pm.test" block finishes executing before the rest call is completed... I haven't found any other way to solve that but using setTimeout... I would try putting a timeout probably in the getApp call

setTimeout(() => { getApp(item.uuid ) },2000)

And see what that does... or maybe around the whole pm.test

setTimeout(() => {
pm.test("Apps", function () {
...
})
) },2000)

EDIT: I came back here because for some reason your issue kept revolving inside my head, and found a few things I think need to be pointed out

  1. Why are you setting up the environment variable inside the pm.test block? I mean, the pm.test block is intended for testing your request's response...
  2. Why is it that you are not asserting anything in your test?
  3. How exactly are you trying to detect that the sendrequest has been executed? By checking if the console.log prints correctly?

@arun-a-nayagam
Copy link

Hi,
@jprealini Thank you for the reply. Apologies, I did find what my issue was but I forgot to come and update here.
Basically, I had to surround my pm.sendRequest around a try, catch block.
If it's not surrounded then it was not writing to the console of an issue with the sendRequest.

And about your questions,

  1. In this instance I don't use the Test block for asserting a test case.
    My intention is to run a script after a postman request to initialize my env vars which I will subsequently use in other API calls.
  2. See 1
  3. Yes, I was expecting the console.log to indicate what the response was.
    But as I said there was an issue with the call and without try/catch there was no information shown in the console.

I really appreciate you replying back and checking! Thank you.

@kiranparajuli589
Copy link

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

like postman.setNextRequest('req-name') but before the actual request not after.

@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