set_scripts
Replaces the scripts of a request, folder or collection.
Overview
set_scripts writes the pre-request script, the post-response script or both for a request, folder or collection. Pass exactly one of requestId, folderId or collectionId. Only the scripts you give change, and an empty string removes one.
{
"requestId": "5B2C8E41-9D3A-4F67-B0E8-1A4C7D2F6E95",
"postResponse": "test(\"returns the new order\", () => {\n expect(relay.response.code).toBe(201);\n relay.environment.set(\"orderId\", relay.response.json().id);\n});"
}Scripts an agent writes don't run until you review them in Relay and click Allow.
Parameters
Prop
Type
Return Value
The scripts as they are now, in the same shape as get_scripts without inherited, plus a note while a script waits for your approval:
{
"kind": "request",
"id": "5B2C8E41-9D3A-4F67-B0E8-1A4C7D2F6E95",
"name": "Create Order",
"preRequest": "",
"postResponse": "test(\"returns the new order\", () => {\n expect(relay.response.code).toBe(201);\n relay.environment.set(\"orderId\", relay.response.json().id);\n});",
"enabled": true,
"approved": false,
"note": "These scripts won't run until the user reviews them and clicks Allow in Relay's Scripts tab."
}Prop
Type
Discussion
Scripts use Relay's scripting API, which follows Postman's: relay (also available as pm), test, expect, assert, req and res, console, timers, top-level await, relay.sendRequest, and libraries such as lodash, CryptoJS, moment and cheerio through require. Values set with relay.environment.set, relay.collectionVariables.set and relay.globals.set are saved. Values set with relay.variables.set last for one send. For the full API, see Scripting API.
With validate on, Relay checks each script for syntax errors and returns the first error, with its line number, without changing anything. Turn validate off only to save a script you'll finish in Relay.
The tool returns an error when you pass more or fewer than one id, when the id doesn't exist, when you give neither preRequest nor postResponse, or when the request, folder or collection is in a read-only or unavailable workspace. A draft's scripts can always be written.
The change isn't added to any window's undo history.
See Also
- get_scripts: Reads the scripts of a request, folder or collection.
- Allowing scripts to run: Review and allow scripts that changed outside Relay before they run.
- Scripting API: The objects and functions available to request scripts.