Skip to content
Vegha Docs

Post-response Scripts

A post-response script runs after the response is received. Use it to inspect the response and feed values into later requests.

Like pre-request scripts, post-response scripts can live on a request, a folder, or a collection. They have access to the res object in addition to bru and req.

The res object represents the response that came back:

console.log(res.status); // e.g. 200
console.log(res.statusText); // e.g. "OK"
console.log(res.responseTime); // milliseconds

The body is available as res.body. For JSON APIs you can read fields directly:

const data = res.body;
console.log(data.id);

The most common use of a post-response script is capturing a value and storing it as a runtime variable so the next request can use it:

// Save the access token returned by a login request
bru.setVar("access_token", res.body.token);

A later request can then reference {{access_token}} in its Authorization tab or headers. This is how you chain a login request into the authenticated requests that follow.

// Capture an id created by a POST to use in a follow-up GET
bru.setVar("order_id", res.body.order.id);

Post-response scripts can branch on the response:

if (res.status === 401) {
bru.setVar("needs_login", true);
}
const items = res.body.items ?? [];
if (items.length > 0) {
bru.setVar("first_item_id", items[0].id);
}

Response headers are available on res.headers:

const requestId = res.headers["x-request-id"];
bru.setVar("trace_id", requestId);

See the Scripting API reference for the full res API.