Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postman scripts : "pm is not defined"

Tags:

postman

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?

like image 432
electrotype Avatar asked Sep 15 '17 16:09

electrotype


People also ask

How do you define pm in Postman?

The pm. request object provides access to the data for the request the script is running within. For a Pre-request Script this is the request that's about to run, and for a Test script this is the request that has already run. You can use the pm.

What is Prerequest script in Postman?

You can use pre-request scripts in Postman to execute JavaScript before a request runs. By including code in the Pre-request Script tab for a request, collection, or folder, you can carry out pre-processing such as setting variable values, parameters, headers, and body data.

Does postman support scripting?

Scripts in Postman You can add JavaScript code to execute during two events in the flow: Before a request is sent to the server, as a pre-request script under the Pre-request Script tab. After a response is received, as a test script under the Tests tab.

What is postman scripting?

Scripting in Postman 1 Scripts in Postman. Postman contains a powerful runtime based on Node.js that allows you to add dynamic behavior to requests and collections. 2 Execution order of scripts. A pre-request script associated with a collection will run prior to every request in the collection. 3 Debugging scripts. ...

What is the difference between pm and postman?

The pm object provides most of the functionality for testing your request and response data, with the postman object providing some additional workflow control. You will carry out most of the Postman JavaScript API functionality using pm.*, which provides access to request and response data, and variables.

How to use variables in Postman using JavaScript?

You will carry out most of the Postman JavaScript API functionality using pm.*, which provides access to request and response data, and variables. You can access and manipulate variables at each scope in Postman using the pm API. You can use dynamic variables to generate values when your requests run. Postman supports a variety of variable scopes.

How do I use postman to debug a test?

When you encounter errors or unexpected behavior in your test scripts, the Postman Console can help you to identify the source. By combining console.log debug statements with your test assertions, you can examine the content of the HTTP requests and responses, as well as Postman data items such as variables.


2 Answers

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.

like image 158
electrotype Avatar answered Oct 10 '22 07:10

electrotype


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.

like image 4
Mike Grace Avatar answered Oct 10 '22 05:10

Mike Grace