Scripting
Javascript Reference

JavaScript API Reference

Here is the complete set of API reference for the scripting feature in Bruno.

Request

This req variable is available inside your scripting and testing context.

Below is the API documentation for the methods available on req

getUrl

Get the current request url

Example:

let url = req.getUrl();

setUrl

Set the current request url

Example:

req.setUrl("https://api.github.com/search/repositories?q=vue");

getMethod

Get the current request method

Example:

const method = req.getMethod();

setMethod

Set the current request method

Example:

req.setMethod("POST");

getHeader

Get the request header by name

Example:

req.getHeader("transaction-id");

getHeaders

Get the current request headers

Example:

const headers = req.getHeaders();

setHeader

Set the request header by name

Example:

req.setHeader( "content-type", "application/json");

setHeaders

Set the current request headers

Example:

req.setHeaders({
  "content-type": "application/json",
  "transaction-id": "foobar"
});

getBody

Get the current request body/payload

Example:

const body = req.getBody();

setBody

Set the request body/payload

Example:

req.setBody({
  "username": "john nash",
  "password": "governingdynamics"
});

setMaxRedirects

Set the maximum number of redirects to follow

Example:

req.setMaxRedirects(5);

Response

This res variable is available inside your scripting and testing context.

Below are the properties available on the res object.

PropertyDescription
statusThe response status code
statusTextThe response status text
headersThe response headers
bodyThe response body
responseTimeThe API response time

Below are the methods available on the res object.

getStatus

Get the response status

Example:

let status = res.getStatus();

getHeader

Get the response header by name

Example:

let transactionId = res.getHeader("transaction-id");

getHeaders

Get the response headers

Example:

let headers = res.getHeaders();

getBody

Get the response data

Example:

let data = res.getBody();

getResponseTime

Get the response time

Example:

let responseTime = res.getResponseTime();

bru

The bru variable is available inside your scripting and testing context. It exposes methods that allow you to interact with, e.g., process variables, environment variables and collection variables.

Below is the API documentation for the methods available on bru

Node process environment

Bruno allows you to get Node process environment variables on the fly.

getProcessEnv

Get the Node process environment variable. This allows secret token usage without committing secrets to version control.

Example:

let secret_token = bru.getProcessEnv("secret_access_token");

Environments

Bruno allows you to get and set Bruno environment variables on the fly.

getEnvVar

Get the Bruno environment variable

Example:

let token = bru.getEnvVar("access_token");

setEnvVar

Set the Bruno environment variable

Example:

function onResponse(res) {
let data = res.getBody();
let token = bru.setEnvVar("access_token", data.token);
}

Collection Variables

Bruno allows you to get and set collection variables on the fly. The collection variables take precendence over environment variables.

getVar

Get the collection variable

Example:

let petId = bru.getVar("petId");

setVar

Set the collection variable

Example:

let data = res.getBody();
bru.setVar("petId", data.id);

Request Order

You can influence the order in which requests are being run by the request-runner (UI) or the CLI.

setNextRequest

By default, the collection runner (UI) and the CLI run requests in order. You can change the order by calling setNextRequest with the name of the next request to be run. This works only in a post-request script or test-script.

Example:

bru.setNextRequest("Get process status");

You can also abort the run by explicitly setting the next request to null Example:

bru.setNextRequest(null);  // aborts the run gracefully