31

I try to write a Pre-request script in Postman. I want to to make a request so I try to use pm.sendRequest. For example :

pm.sendRequest('http://example.com', function (err, res) {
    //...
});

But I get this error :

There was an error in evaluating the Pre-request Script: pm is not defined

I'm on Windows 10. I just updated the extension.

How do I access pm?

electrotype
  • 8,342
  • 11
  • 59
  • 96

5 Answers5

57

A member posted an answer but, for some reason, I think he got banned. His answer didn't have a lot of details, but was working :

You have to use the standalone version of Postman for the pm to be accessible. I was using it as a Chrome extension. By switching to the standalone version, it worked. I don't know why, though.

electrotype
  • 8,342
  • 11
  • 59
  • 96
  • Thanks! I didn't' realize my installation of PostMan was really old. And yes, it was the Chrome app. After I installed the full standalone version, the pm object works. – duyn9uyen Oct 31 '17 at 19:10
4

If you are running an old version of Postman, you may run into this issue as I did. From https://www.getpostman.com/docs/v6/postman/scripts/test_examples#older-style-of-writing-postman-tests

The older style of writing Postman tests relies on setting values for the special tests object.

If you are running an older version of Postman, you will not have the pm object available for use.

Mike Grace
  • 16,636
  • 8
  • 59
  • 79
0

For extension version, you can use following pattern:

tests["Status code is 200"] = responseCode.code === 200;

0

Update

As per the postman's official doc, using postman.* or responseBody are deprecated and were old instructions. So, make sure you are running the latest version of Postman (standalone or chrome extension).

However, if you still face issues with pm.*, then proceed below for some help.

Previous

If anyone is using the Desktop version of Postman (in my case Mac's desktop version),

Then,

  • use responseBody instead of pm.response.text()
  • to set global variable postman.setGlobalVariable() instead of pm.environment.set()

Sample:

var jsonData = JSON.parse(responseBody);
postman.setGlobalVariable("studentId", jsonData.studentId);

You can use SNIPPETS to auto-generate basic things like the above to avoid these errors. enter image description here

My Postman version detail:

Postman for Chrome
Version 5.5.6
OS X / arm64
Chrome 108.0.0.0
  • I´m also using chrome extension. Is there an equivalent for ```pm.sendRequest```? – Jorge Mauricio Mar 18 '23 at 13:19
  • Please make sure that you are running the latest version of the postman. Refer: [postman doc](https://learning.postman.com/docs/writing-scripts/script-references/test-examples/#previous-style-of-writing-postman-tests-deprecated) – Karthikaiselvan R Mar 19 '23 at 19:45
  • Below might be helpful for you, ```var settings = { "async": true, "crossDomain": true, "url": environment.ThirdPartyUrl + "/authorize/refresh/?AppKey=" + environment.ThirdPartyApiKey, "method": "GET" }; $.ajax(settings).done(function (response) { postman.setGlobalVariable("AccessToken", response.AccessToken); });``` Refer: [medium article](https://mrponath.medium.com/postman-pre-request-scripts-81920a78d0fc) – Karthikaiselvan R Mar 19 '23 at 19:48
-3

Replace pm with postman, it should work fine.