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.
Where post-response scripts run
Section titled “Where post-response scripts run”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.
Reading the response with res
Section titled “Reading the response with res”The res object represents the response that came back:
console.log(res.status); // e.g. 200console.log(res.statusText); // e.g. "OK"console.log(res.responseTime); // millisecondsThe body is available as res.body. For JSON APIs you can read fields directly:
const data = res.body;console.log(data.id);Extracting values for request chaining
Section titled “Extracting values for request chaining”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 requestbru.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 GETbru.setVar("order_id", res.body.order.id);Conditional logic
Section titled “Conditional logic”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);}Reading headers
Section titled “Reading headers”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.