Debug pre-request and test scripts with console.log

Scripts are small, but they fail in confusing ways: a test that says Expected undefined to equal 42, a token that's set but never used, a variable that has the right value in one request and the wrong one in the next. The fastest way through is to print things.

Where the output goes

Where you runWhere to look
Desktop and web app, SendThe diagnostics drawer: console logs, script output, issues, network history
Collection runner in the appExpand a request's row in the results: tests, warnings, script console and response
CLI run / run-collection--verbose prints script console output with each request
CLI --json / --outEach request result has a console array
Saved runs and reportsThe HTML report shows each request's tests and response

Log objects, not [object Object]

JavaScript
const body = pm.response.json();
console.log('response', JSON.stringify(body, null, 2));

For large responses, log the part you care about:

JavaScript
console.log('first item', JSON.stringify(body.items?.[0]));
console.log('item count', body.items?.length);

Label every line

When a collection has a collection script, a folder script and a request script for the same phase, they all run, in that order: collection, folder, then request. Without labels you can't tell which one logged what:

JavaScript
console.log('[folder: Orders] token set?', Boolean(pm.environment.get('token')));

Find out where a variable comes from

A value can come from collection variables, folder variables, the environment, a local .env file, a data row or an earlier script. Print what the script sees:

JavaScript
console.log('environment', pm.environment.get('baseUrl'));
console.log('merged', pm.variables.get('baseUrl'));
console.log('resolved URL', pm.variables.replaceIn('{{baseUrl}}/orders'));

If the environment value and the merged value differ, something with a higher priority is overriding it. In the app, the Variables panel of the request shows each {{variable}} it uses, its value and its source.

Debug a failing test

Expected undefined to equal 42 means the path is wrong, not the value. Log what you're testing before you test it:

JavaScript
const order = pm.response.json();
console.log('order keys', Object.keys(order));
pm.test('total is 42', () => pm.expect(order.total).to.equal(42));

The keys usually show the problem straight away: totalAmount, not total, or the data wrapped in { "data": { … } }.

Debug a request that's built wrong

In a pre-request script, log what will be sent after your changes:

JavaScript
pm.request.headers.set('X-Request-Id', require('uuid').v4());
console.log('url', pm.request.url.toString());
console.log('headers', JSON.stringify(pm.request.headers.toJSON()));
console.log('body', pm.request.body.raw);

Then compare with the network history in the diagnostics drawer, which shows what actually went out.

Warnings for things that aren't failures

console.warn is good for "this works, but look at it": a deprecated endpoint, a slow response, a missing optional field.

JavaScript
if (pm.response.responseTime > 1000) {
  console.warn(`slow: ${pm.response.responseTime} ms`);
}

In CI

--verbose makes logs long, so turn it on when you need it:

Terminal
requestarium run-collection "Orders API" --env Staging --verbose

Or keep CI output short and look at the console of one request in the JSON report:

Terminal
requestarium run-collection "Orders API" --env Staging --out report.json
jq '.results[] | select(.status != "passed") | {path, console}' report.json

Secret values are masked in the CLI's reports and output, but response bodies are printed as received. Don't log tokens or personal data you wouldn't want in a CI log.