When a script runs#
JavaScript runs before the send and after the response, both through one pm object.
Scripts let a request do work around the send. A pre-request script runs immediately before the request goes out. It can change the request or set up variables. A test script runs as soon as the response comes back. It can check the response and save values for later. You write both in JavaScript against a small pm API.
The two phases#
Scripts live on two tabs in the request panel:
- Pre-request. Runs before the request goes out. Use it to compute a value, to set a variable, or to adjust the outgoing request. If it throws, GraphDagger does not send the request.
- Tests. Runs after the response arrives. Use it to assert that the response is correct, and to save values from it. A failed test does not undo the request. GraphDagger records it as a failure.
The pm API#
Both phases share one object, pm. A script reads and changes things through it:
pm.requestreads and, in a pre-request script, edits the outgoing request.pm.responsereads the response, in a test script.pm.environment,pm.globals, andpm.collectionVariablesread and write variables that persist after the script ends.pm.variablesreads across every scope and can resolve{{...}}tokens in a string.pm.testandpm.expectregister tests and assertions.console.logand its variants write to the Console.
The scripting reference holds the full list. As you type, the editor suggests members and offers ready-made snippets for common tasks.
See what a script did#
Two response tabs report on scripts:
- Test results shows each test with a green tick or a red cross, and a pass count in the tab label.
- Console shows anything your script logged, each line stamped with the time and level. The tab stays greyed out until there is something to show.
Together they let you chain requests. Read a token from one response in a test script, save it to an environment variable, then reference it with {{token}} in the next request.