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
?
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.
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.
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.
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. ...
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With