Scripting API Reference
This page lists the objects and functions available inside pre-request and post-response scripts. All of them run inside the Jint sandbox.
The bru object reads and writes variables and the environment.
| Member | Description |
|---|---|
bru.getVar(name) | Returns the value of a variable, resolving across all scopes |
bru.setVar(name, value) | Sets a runtime variable for the current run |
bru.getEnvVar(name) | Returns a value from the active environment |
bru.setEnvVar(name, value) | Sets a value in the active environment |
const base = bru.getEnvVar("base_url");bru.setVar("health_url", base + "/health");See Variable types for how scopes and precedence interact.
The req object represents the outgoing request. It is available in both pre-request and post-response scripts; mutating it in a pre-request script changes what is sent.
| Member | Description |
|---|---|
req.url | The request URL — readable and writable |
req.method | The HTTP method, for example GET or POST |
req.headers | The request headers, as a key/value map |
req.body | The request body |
req.headers["X-Request-Id"] = bru.getVar("request_id");The res object represents the response. It is available only in post-response scripts and the Tests tab.
| Member | Description |
|---|---|
res.status | The numeric HTTP status code |
res.statusText | The HTTP status text, for example OK |
res.headers | The response headers, as a key/value map |
res.body | The response body (parsed for JSON responses) |
res.responseTime | The total response time in milliseconds |
bru.setVar("access_token", res.body.token);test and expect
Section titled “test and expect”Used in the Tests tab to write assertions. See Assertions & tests for a fuller guide.
| Function | Description |
|---|---|
test(name, fn) | Defines a named test; fn contains the assertions |
expect(value) | Begins an assertion chain on value |
Common expect chains:
| Chain | Asserts |
|---|---|
.to.equal(x) | Value equals x |
.to.exist | Value is defined and not null |
.to.include(x) | String or array contains x |
.to.be.below(n) | Number is less than n |
.to.have.lengthOf(n) | Array or string has length n |
test("status is 200", () => { expect(res.status).to.equal(200);});