Test APIs with Postman - Scripting
We can set pre-request scripts (run before the request) & tests (after execution) at several levels:
- Collection
- Folder
- Request
Snippets
Inside pre-request Script and Tests we have a SNIPPETS column with templates we may use for our code.
Get / Set variables
console.log("Hello world");
// work with local vars
let urlVar = pm.variables.get("protocol");
console.log("value for protocol: " + urlVar);
pm.variables.set("protocol", "http");
console.log(pm.variables.get("protocol"));
// work with global vars
let globalVar = pm.globals.get("env");
console.log(globalVar);