Shared State

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

  1. 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.

  2. 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.

  3. Manage tokens or values across different API requests.

How to use?

  1. Create a new modify response rule or modify request rule

  2. Select dynamic javascript modification

  3. You can use the $sharedState variable to manage and share state

    1. Using $sharedState to store data

      function 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;
      }
      
    2. Using $sharedState to dynamically modify the response

      function 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;
      }
      

FAQs

Updated on