You can use javascript code to add dynamic behaviour to your API client requests. Use pre-request and post-response scripts to pass data between requests or build requests with dynamic parameters.
-
Pre-request scripts: These scripts are executed before a request is sent to the server.
-
Post-response scripts: These scripts are executed after receiving the response.
Using variables in scripts
The objects and methods are exposed through the rq
object. It can be used to access and set the current environment.
-
Accessing current environment variables
// environment var 'target' = 2 // accessing the value console.log(rq.environment.get('target')); // outputs 2 rq.environment.set('target', 3); //sets target variable in current environment with value 3 rq.environment.unset('target') //removes target variable from current environment
Using request and response data in scripts
You can use rq.request
in pre-request script and rq.response
in post-response script to access request and response data in the scripts. Currently, the following methods are supported, with additional methods planned to be added soon.
// accessing request details
rq.request.method
rq.request.headers
rq.request.body // access request body
rq.request.url
rq.request.queryParams
//accessing response details
rq.response.body // response body string
rq.response.time // response time
rq.response.headers
rq.response.status
rq.response.statusText
Debugging scripts
If there are any errors in the scripts, they will be displayed immediately. You can debug the scripts further using the browser console.