Scripting
Request
Request Object

Request Object

The req object represents the HTTP request made to the server. It contains various properties that define the request details.

req Sub-Object

The req sub-object contains detailed information about the request.

  • assertions : An array containing any assertions associated with the request. See assertions.
  • auth : An object containing authentication credentials, such as username and password.
  • headers : A sub-object representing the HTTP headers associated with the request.
  • method : The HTTP method used for the request (e.g., "GET", "POST").
  • mode : The mode of the request (e.g., "none", "cors").
  • responseType : The expected response type for the request (e.g., "text", "json").
  • script : An object containing script-related information for the request.
  • signal : A signal object used to abort the request.
  • url : The URL of the request.
  • vars : An object containing any variables associated with the request. See variables.

Headers

The headers sub-object of the req object contains key-value pairs representing the HTTP headers associated with the request.

// Example usage
console.log(req.headers);
/* Output: {
  authorization: 'Bearer <token>',
  'content-type': 'application/json',
  accept: 'application/json',
  // Add more headers as needed...
} */

Method

The method property of the req object specifies the HTTP method used for the request. Common HTTP methods include "GET", "POST", "PUT", "DELETE", etc. The method indicates the type of action the request wishes to perform on the resource. The value of the method property should be a string representing the desired HTTP method for the request.

// Example usage
console.log(req.method); // Output: "GET"

URL

The url property of the req object represents the Uniform Resource Locator (URL) of the request. It specifies the address of the resource being requested by the client. Variables enclosed within double curly braces ({{...}}) in the URL string are placeholders that may be replaced with actual values at runtime. These variables are not directly visible within the URL string and are typically encapsulated during request processing.

// Example usage
console.log(req.url); // Output: "{{base.url}}/users/2?queryTest=queryResult"

Example Usage

// Example request object
const req = {
  assertions: [],
  auth: { username: 'myUsername', password: 'mySuperPassword' },
  headers: {
    authorization: 'Bearer <token>',
    'content-type': 'application/json',
    accept: 'application/json',
    // Add more headers as needed...
  },
  method: 'GET',
  mode: 'none',
  responseType: 'arraybuffer',
  script: {
    req: "// Create an array of objects\nconst data = [\n  { i…q);\nconst myVariable = bru.getEnvVar('password');"
  },
  signal: {},
  url: '{{base.url}}/users/2?queryTest=queryResult',
  vars: {}
};
 
 
// Accessing request properties
console.log(req.method); // Output: "GET"
console.log(req.url); // Output: "{{base.url}}/users/2?queryTest=queryResult"
console.log(req.headers.authorization); // Output: "Bearer <token>"
console.log(req.auth.username); // Output: "myUsername"