This is only available in extension version > 24.8.13
and desktop app > 1.7.1
$sharedState
allows you to store and retrieve data within and across Modify Request and Modify Response rules. It is a javascript Object where you can assign key-value pairs from different requests and use it in your rules.
Use cases
-
Conditional request/response modification based on some conditions stored in previous requests. Please read here about how you can conditionally fail requests based on requests counts.
-
Aggregate data from multiple requests to be used in a rule. Read here to know about how you can use shared state to aggregate data and use across Requestly rules.
-
Manage tokens or values across different API requests.
How to use?
-
Create a new modify response rule or modify request rule
-
Select dynamic javascript modification
-
You can use the
$sharedState
variable to manage and share state-
Using
$sharedState
to store datafunction modifyResponse(args) { const {method, url, response, responseType, requestHeaders, requestData, responseJSON} = args; // Stores all Request URLs and their response const URLMap=$sharedState.urlMap; URLMap[url]=response; $sharedState.urlMap=urlMap; return response; }
-
Using
$sharedState
to dynamically modify the responsefunction modifyResponse(args) { const {method, url, response, responseType, requestHeaders, requestData, responseJSON} = args; // Checks if requestURL: https://api.ex.com/postData was fired // then fail all the subsequent requests if("https://api.ex.com/postData" in $sharedState.urlMap){ return null; } return response; }
-