Request
Thereq variable represents the HTTP request object and is automatically available inside your scripting and testing context. It provides methods to access and modify the current request’s properties such as URL, method, headers, body, and other configuration options before the request is sent to the server.
The
req object is available in pre-request scripts and test scripts, allowing you to modify request properties before execution and access them after completion.req object.
| Method | Description |
|---|---|
| req.getUrl() | Get the current request URL. |
| req.setUrl(url) | Set the current request URL. |
| req.getAuthMode() | Get the current authentication mode. |
| req.getMethod() | Get the current request method. |
| req.setMethod(method) | Set the current request method. |
| req.getName() | Get the current request name. |
| req.getTags() | Get the current request tags as an array of strings. |
| req.getHeader(name) | Get the request header by name. |
| req.getHeaders() | Get all request headers. |
| req.setHeader(name, value) | Set a request header by name. |
| req.setHeaders(headers) | Set multiple request headers. |
| req.getBody(options?) | Get the current request body/payload (supports raw option). |
| req.setBody(body) | Set the request body/payload. |
| req.setMaxRedirects(count) | Set the maximum number of redirects to follow. |
| req.getTimeout() | Get the current timeout value of the request. |
| req.setTimeout(milliseconds) | Set a timeout for the request. |
| req.getExecutionMode() | Get the current active execution mode (runner or standalone). |
| req.getExecutionPlatform() | Get the platform on which the request is being executed (app or cli). |
| req.onFail(callback) | Handle request errors with a custom callback function. |
req
URL Methods
-
getUrl()- Get the current request URL. -
setUrl(url)- Set the current request URL.
HTTP Method
-
getMethod()- Get the current request method. -
setMethod(method)- Set the current request method.
Request Information
-
getName()- Get the current request name. -
getAuthMode()- Get the current authentication mode. -
getTags()- Get the current request tags as an array of strings. This method allows you to access the tags associated with the current request, which can be useful for conditional logic, filtering, or organizing requests based on their tags. Returns: Array of strings representing the request tags
Header Methods
-
getHeader(name)- Get the request header by name. -
getHeaders()- Get all request headers. -
setHeader(name, value)- Set a request header by name. -
setHeaders(headers)- Set multiple request headers.
Body Methods
-
getBody(options?)- Get the current request body/payload. Parameters:options(object, optional): Configuration optionsraw(boolean): Whentrue, returns the raw body without any parsing. Whenfalseor not provided, returns the parsed body (default behavior).
-
setBody(body)- Set the request body/payload.
Request Configuration
-
setTimeout(milliseconds)- Set a timeout for the request. -
getTimeout()- Get the current timeout value of the request. -
setMaxRedirects(count)- Set the maximum number of redirects to follow.
Execution Context
-
getExecutionMode()- Get the current active execution mode of the request. Returns:runner: When the request is being executed as part of a collection runstandalone: When the request is being executed individually
-
getExecutionPlatform()- Get the platform on which the request is being executed. Returns:app: When running in the Bruno desktop applicationcli: When running through the Bruno CLI
Error Handling
-
onFail(callback)- Handle request errors with a custom callback function. This is useful for implementing custom error handling logic, logging, or taking specific actions when a request fails. Parameters:error: The error object containing details about the request failure
The
onFail function is only available in Developer mode and should be called in pre-request scripts. When using Bruno CLI (v3.0.0+), pass the --sandbox=developer flag.Response
Theres variable represents the HTTP response object and is automatically available inside your scripting and testing context after a request is executed. It contains all the information about the response received from the server, including status codes, headers, body data, and timing information.
The
res object is only available in post-request scripts and test scripts, as it contains the response data from the completed request.res object.
| Property / Method | Description |
|---|---|
| res.status | The HTTP response status code (e.g., 200, 404, 500). |
| res.statusText | The HTTP response status text (e.g., “OK”, “Not Found”). |
| res.headers | An object containing all response headers. |
| res.body | The response body data (automatically parsed as JSON if applicable). |
| res.responseTime | The total time taken for the request in milliseconds. |
| res.url | The final response URL (after following redirects). |
| res.getStatus() | Get the response status code. |
| res.getStatusText() | Get the response status text. |
| res.getHeader(name) | Get a specific response header by name. |
| res.getHeaders() | Get all response headers. |
| res.getBody(options?) | Get the response body data (supports raw option). |
| res.setBody(body) | Set the response body data. |
| res.getResponseTime() | Get the response time in milliseconds. |
| res.getUrl() | Get the response URL (final URL after redirects). |
| res.getSize() | Get the response size in bytes (returns object with body, headers, total). |
The
body property is automatically parsed as JSON if the response has a JSON content type. For other content types, it will be a string.res object.
Response Properties
-
res.status- The HTTP response status code (e.g., 200, 404, 500). -
res.statusText- The HTTP response status text (e.g., “OK”, “Not Found”, “Internal Server Error”). -
res.headers- An object containing all response headers. -
res.body- The response body data (automatically parsed as JSON if the response has a JSON content type). -
res.responseTime- The total time taken for the request in milliseconds. -
res.url- The final response URL (after following redirects).
Status Methods
-
getStatus()- Get the response status code. -
getStatusText()- Get the response status text.
Header Methods
-
getHeader(name)- Get a specific response header by name. -
getHeaders()- Get all response headers.
Body Methods
-
getBody(options?)- Get the response body data. Parameters:options(object, optional): Configuration optionsraw(boolean): Whentrue, returns the raw response body without any parsing. Whenfalseor not provided, returns the parsed body (default behavior).
-
setBody(body)- Set the response body data.
URL Methods
-
getUrl()- Get the response URL. In case of redirects, you will get the final URL which may be different from the original request URL if redirects were followed. This functionality is also available as a propertyres.url. Returns: Response URL as a string. Example:
Timing & Size Methods
-
getResponseTime()- Get the response time in milliseconds. -
getSize()- Get the response size in bytes. Returns an object with the following properties:body: numberheaders: numbertotal: number
Environments
Thebru object provides methods for managing environments in Bruno. Environments allow you to maintain different configurations for various deployment stages (development, staging, production, etc.).
Environment variables are specific to the selected environment and can be accessed across all requests in your collection.
| Method | Description |
|---|---|
| bru.getEnvName() | Retrieves the environment name. |
| bru.hasEnvVar(key) | Checks if the environment variable exists. |
| bru.getEnvVar(key) | Retrieves the value of an environment variable. |
| bru.setEnvVar(key, value) | Sets a new environment variable. |
| bru.deleteEnvVar(key) | Deletes a specific environment variable. |
| bru.getGlobalEnvVar(key) | Get the Bruno global environment variable. |
| bru.setGlobalEnvVar(key, value) | Set the Bruno global environment variable. |
Environment Information
getEnvName()- Retrieves the current environment name.
Environment Variables
-
getEnvVar(key)- Get the Bruno environment variable. -
setEnvVar(key, value, options?)- Set the Bruno environment variable. By default, environment variables are temporary and do not persist between app restarts. Parameters:key(string): The environment variable namevalue(any): The value to setoptions(object, optional): Configuration optionspersist(boolean): Whentrue, saves the variable to file system. Whenfalseor not provided, maintains in-memory behavior.
-
hasEnvVar(key)- Check if the environment variable exists. -
deleteEnvVar(key)- Delete a specific environment variable.
Global Environment Variables
-
getGlobalEnvVar(key)- Get the Bruno global environment variable. -
setGlobalEnvVar(key, value)- Set the Bruno global environment variable.
Variables
Bruno provides a comprehensive variable system that allows you to manage and access different types of variables throughout your scripts. Variables are resolved in a specific order of precedence, with runtime variables taking the highest priority.Variable precedence (highest to lowest): Runtime Variables → Request Variables → Folder Variables → Collection Variables → Environment Variables
| Method | Description |
|---|---|
| bru.getProcessEnv(key) | Fetches the process environment variable for a given key. |
| bru.getCollectionVar(key) | Retrieves the collection-level variable for the key. |
| bru.getCollectionName() | Retrieves the current collection name. |
| bru.getFolderVar(key) | Fetches a folder-specific variable by key. |
| bru.getRequestVar(key) | Retrieves the value of a request variable. |
| bru.hasVar(key) | Checks if a variable exists. |
| bru.getVar(key) | Retrieves the value of a variable. |
| bru.setVar(key, value) | Sets a new variable with a key-value pair. |
| bru.deleteVar(key) | Deletes a specific variable. |
| bru.deleteAllVars() | Deletes all runtime variables. |
| bru.getOauth2CredentialVar(key) | Retrieves an OAuth2 credential variable value. |
Process Environment Variables
getProcessEnv(key)- Get the Node process environment variable. This allows secret token usage without committing secrets to version control.
Collection Variables
-
getCollectionVar(key)- Get the collection variable. -
getCollectionName()- Retrieve the name of the current collection.
Folder Variables
getFolderVar(key)- Get the folder variable.
Request Variables
getRequestVar(key)- Get the request variable.
Runtime Variables
Runtime variables are temporary variables that exist only during the execution of your requests. They are commonly used to pass data between requests in a collection run.-
hasVar(key)- Check if a runtime variable exists. -
getVar(key)- Get the runtime variable. -
setVar(key, value)- Set the runtime variable. -
deleteVar(key)- Delete the runtime variable. -
deleteAllVars()- Delete all runtime variables.
OAuth2 Credentials
getOauth2CredentialVar(key)- Retrieve an OAuth2 credential variable value. This is useful for accessing OAuth2 tokens and credentials.
Runner
The Runner API provides methods to control the execution flow of your collection runs. These methods are specifically designed for use within the collection runner context, allowing you to skip requests, change execution order, or stop the run entirely. Here are all available runner-related methods:| Method | Description |
|---|---|
| bru.setNextRequest(requestName) | Sets the next request to execute. |
| bru.runner.setNextRequest(requestName) | Alter the order of requests by specifying the next request. |
| bru.runner.skipRequest() | Skip the execution of the current request. |
| bru.runner.stopExecution() | Terminate a collection run. |
Controlling Execution Order
-
setNextRequest(requestName)- Change the order of request execution. By default, the collection runner (UI) and the CLI run requests in order. You can change the order by callingsetNextRequestwith the name of the next request to be run. This works only in a post-request script or test-script. How it works:setNextRequestworks with single requests - it executes the current request first, then jumps to the specified request- It skips all requests between the current one and the target request
- The target request must exist in the collection and be specified by its exact name
- For requests inside folders: Use just the request name (e.g., “request-name”), not the full path
- Works the same way with both
bru.setNextRequest()andbru.runner.setNextRequest()
-
bru.runner.setNextRequest(requestName)- Alter the order of requests by specifying the name of the next request to execute. This function is applicable only within post-request scripts or test scripts.
Collection Runner Control
-
bru.runner.skipRequest()- Skip the execution of the current request. Use this in the pre-request script section. This function is valid only within the context of a collection run. -
bru.runner.stopExecution()- Terminate a collection run. This function can be called from a pre-request, post-response, or test script and only takes effect during a collection run.
Utilities
The Utilities API provides a collection of helper functions for common tasks such as making HTTP requests, working with cookies, managing tests, and other utility operations. Here are all available utility methods:| Method | Description |
|---|---|
| bru.sendRequest(options, callback) | Sends a programmatic HTTP request within your script. |
| bru.sleep(milliseconds) | Pauses execution for the specified duration. |
| bru.interpolate(string) | Evaluates dynamic variables within a string. |
| bru.disableParsingResponseJson() | Disables JSON response parsing for the request. |
| bru.cwd() | Returns the current working directory. |
| bru.runRequest(requestPathName) | Executes a request by its path name. |
| bru.getAssertionResults() | Retrieves the results of assertions. |
| bru.getTestResults() | Fetches the test results. |
| bru.cookies.jar() | Creates a cookie jar instance for managing cookies. |
HTTP Request Methods
-
sendRequest(options, callback)- Send a programmatic HTTP request within your script. This allows you to make additional API calls during script execution. Parameters:method: HTTP method (GET, POST, PUT, etc.)url: The URL to send the request toheaders: (Optional) Request headersdata: (Optional) Request data. Can be a string or objecttimeout: (Optional) Request timeout in millisecondscallback: Function to handle the response with signature(err, res)
Helper Functions
-
sleep(milliseconds)- 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. -
interpolate(string)- Evaluates dynamic variables and environment variables within a string. This function allows you to use Bruno’s dynamic variables (like{{$randomFirstName}}) directly in your scripts. -
disableParsingResponseJson()- Prevent the automatic parsing of the JSON response body and work directly with the raw data. Use this in the pre-request script of the request. -
cwd()- Returns the current working directory.
Request Execution
-
runRequest(requestPathName)- Execute any request in the collection and retrieve the response directly within the script. Example:
Test Utilities
-
getTestResults()- Obtain the test results of a request. Use this within test scripts. -
getAssertionResults()- Obtain the assertion results of a request. Use this within test scripts.
Cookie Management
Bruno provides Cookie Jar APIs that enable programmatic cookie management in request scripts. These APIs allow you to set, get, and delete cookies programmatically using a cookie jar instance.Cookie management APIs are available in pre-request scripts, post-request scripts, and test scripts.
Creating a Cookie Jar
-
cookies.jar()- Create a cookie jar instance for managing cookies. Returns: A cookie jar instance with methods for cookie management
Cookie Jar Methods
Once you have a cookie jar instance, you can use the following methods:-
jar.setCookie(url, name, value)orjar.setCookie(url, cookieObject)- Set a single cookie with specified attributes. This method has two overloads for different use cases. Parameters (Key-value format):url: The URL for which the cookie should be setname: The name of the cookievalue: The value of the cookie
url: The URL for which the cookie should be setcookieObject: Cookie object with properties:key,value,domain,path,expires,maxAge,secure,httpOnly,sameSite
-
jar.setCookies(url, cookies)- Set multiple cookies at once using an array of cookie objects. Parameters:url: The URL for which the cookies should be setcookies: Array of cookie objects
-
jar.getCookie(url, name)- Get a specific cookie by name. Returns: The cookie object ornullif not found Example: -
jar.getCookies(url)- Get all cookies for a specific URL. Returns: Array of cookie objects or empty array if no cookies found Example: -
jar.deleteCookie(url, name)- Delete a specific cookie by name. Example: -
jar.deleteCookies(url)- Delete all cookies for a specific URL. -
jar.clear()- Clear all cookies from the cookie jar.