How to use shared state to aggregate data and use across modify request/ modify response rule

Mock an API request body to use data from the response of another API. This can be done by aggregating the response body in $sharedState.

Note: $sharedState is only available in extension version > 24.8.13 and desktop app > v1.7.1

Steps to aggregate data in modify response rule

  1. Create a new modify response rule

  2. Select dynamic javascript modification

  3. Write javascript code to aggregate the data from response body in $sharedState

    function modifyResponse(args) {
      const {method, url, response, responseType, requestHeaders, requestData, responseJSON} = args;
      // Change response below depending upon request attributes received in args
      const productList=responseJSON.products;
      const currency=responseJSON.currency;
      // Store data in $sharedState
      $sharedState.productList=productList;
      $sharedState.currency=currency;
    
      return response;
    }
    

Step to use the aggregated data in modify request body

  1. Create a new modify request body rule

  2. Select dynamic javascript modification

  3. Write javascript code to aggregate the data from response body in $sharedState

    function modifyRequestBody(args) {
      const { method, url, body, bodyAsJson } = args;
      //Retreive data from $sharedState
      const products = $sharedState.productList;
      const currency = $sharedState.currency;
    
      return {
        ...bodyAsJson,
        products,
        currency
      }
    }
    
Updated on