Scripts in Requestly allow you to extend and customize your API requests and responses dynamically using JavaScript. These scripts enable you to manipulate requests before they are sent (Pre-request scripts) or process responses after they are received (Post-response scripts). With access to the full request and response objects, you can achieve advanced automation, validations, and transformations.
Pre-Request Scripts
Pre-Request Scripts run before the API request is sent to the server. They allow you to modify request attributes, such as headers, body, query parameters, or even the URL. Pre Scripts are useful for adding authentication tokens, generating timestamps, or altering the request dynamically based on certain conditions.
{{page_number}} to get value of page number.
Post-Response Scripts
Post-Response Scripts run after the API response is received. They allow you to process response data, validate outputs, or log details for debugging. Post Scripts are useful for transforming the response body, validating response codes, or storing results for further use.
Requestly JavaScript API rq
Requestly provides a robust set of JavaScript properties and methods to interact with API requests, responses, environments, and global variables. Below is a detailed documentation of these features, explaining each property and function with examples.
Request Object rq.request
These properties and methods let you access the details of the API request in scripts.
rq.request.method
Use this property to get the Request’s method. The HTTP method of the request (e.g. GET, POST, PUT, OPTION, DELETE, PATCH, HEAD).
Example:
rq.request.headers
An list of header rows each having key, value & isEnabled for each custom header you create.
Example:
rq.request.body
The body of the request, accessible as a string. If you have selected Form under Body it would be returned in object format.
Example:
rq.request.url
The full URL of the API request.
Example:
rq.request.queryParams
An object containing rows of query parameters each having key, value & isEnabled.
Example:
Response Object rq.response
These properties and methods let you access the details of the API’s response in scripts.
rq.response.body
The body of the response as a string.
Example:
rq.response.responseTime
The time taken by the request to complete, in milliseconds.
Example:
rq.response.headers
An list of header rows each having key, value for each custom header you create.
Example:
rq.response.code
The HTTP status code of the response.
Example:
rq.response.json()
Parses the response body as JSON and returns it as a JavaScript object.
Example:
rq.response.text()
Returns the response body as a plain string.
Example:
Environment Variables Object rq.environment
Environment methods let you dynamically manage environment variables during script execution.
rq.environment.set(key, value)
Sets an environment variable with the given key and value.
Example:
rq.environment.get(key)
Retrieves the value of the specified environment variable.
Example:
rq.environment.unset(key)
Removes the specified environment variable.
Example:
Collection Variables Object rq.collectionVariables
Collection variables are scoped to a specific collection. Unlike environment variables (scoped to a specific environment), collection variables are only accessible within the requests that belong to a given collection.
rq.collectionVariables.set(key, value)
Creates or updates a collection variable with the given key and value.
Example:
rq.collectionVariables.get(key)
Retrieves the value of the specified collection variable.
Example:
rq.collectionVariables.unset(key)
Removes the specified collection variable.
Example:
Global Variables Object rq.globals
Global variables work similarly to environment variables. Global variables are available to all collections and requests.
rq.globals.set(key, value)
Sets a global variable with the given key and value.
Example:
rq.globals.get(key)
Retrieves the value of the specified global variable.
Example:
rq.globals.unset(key)
Removes the specified global variable.
Example:

