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();
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);