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.
Property | Description |
---|---|
status | The response status code |
statusText | The response status text |
headers | The response headers |
body | The response body |
responseTime | The 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
Helpers
sleep
Pauses execution for the specified duration. This is useful for introducing delays or waiting for a specific amount of time before proceeding with the next operation.
Example:
await bru.sleep(3000);
disableParsingResponseJson
To prevent the automatic parsing of the JSON response body and work directly with the raw data, you can use the expression below in the pre-request script of the request.
Example:
bru.disableParsingResponseJson();
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 collection variables on the fly.
getCollectionVar
Get the collection variable
Example:
let namespace = bru.getCollectionVar("namespace");
Folder Variables
Bruno allows you to get folder variables on the fly.
getFolderVar
Get the folder variable
Example:
let company = bru.getFolderVar("company");
Runtime Variables
Bruno allows you to get, set and delete runtime variables on the fly.
getVar
Get the runtime variable
Example:
let petId = bru.getVar("petId");
setVar
Set the runtime variable
Example:
let data = res.getBody();
bru.setVar("petId", data.id);
deleteVar
Delete the runtime variable
Example:
bru.deleteVar("petId");
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