How to print the Pattern used to extract Variable

Hello ,

I have a requirement to reserve the Pattern that will be used to extract a feild using Extract variable policy. For example , I want to get the Pattern out printed ( in my case

/user/details-list/{ID}/checkout ) so that i can use in the later step in the flow. I can get any of the

proxy.pathsuffix as below so need to extract some values out of it . When used , i only see

urirequest.ID or urirequest.CID etc

What should i do to get the Pattern aswell that is used to extract some feild out ? I want to preserve the pattern path for later use to replace some feilds from the next steps output in the flow.

<ExtractVariables name="ExtractVariables-1">
  <DisplayName>Extract a portion of the url path</DisplayName>
  <Source>request</Source>
  <URIPath>
    <Pattern ignoreCase="true">/user/details-list/{ID}/checkout</Pattern>
  </URIPath>
  <URIPath>
    <Pattern ignoreCase="true">/user/details-list/{ID}/balance</Pattern>
  </URIPath>
  <URIPath>
    <Pattern ignoreCase="true">/user/details-list/{CID}/checkout/stop-calculation/{SigID}</Pattern>
  </URIPath>
  <URIPath>
    <Pattern ignoreCase="true">/user/{COID}/profile</Pattern>
  </URIPath>
  <VariablePrefix>urirequest</VariablePrefix>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</ExtractVariables>
0 8 494
8 REPLIES 8

Can you explain more with an example?

I think he wants to print the actual regex of the pattern: /user/details-list/{ID}/checkout

That’s correct, I want to get the expression used to extract variable itself. I have multiple URI paths that proxy gets called with. And to reconstruct the target URL , I am ending up in using many Assign messages with conditional checks both at Extract variable and also at Assign Message policy in the default Proxy endpoint . There should be some better way to handle is what I am thinking.

For this you may need to use to multiple extract variable policies in each having a different pattern. I would use javascript policy instead to get the pattern matching with condition settings.

Hi Sillan: looks like the conditional statements won’t work with in Java Script. My initial thought was Java script - what would be the code you think will look with in Java script ? Path suffix MatchLike won’t work in JavaScript . Can you please guide.

You can use multiple patterns and apply the Extract Variables policy in multiple locations.

For example

<URIPath>
    <Pattern ignoreCase="true">/user/details-list/{CID}</Pattern>
    <Pattern ignoreCase="true">/user/details-list/{CID}/checkout/stop-calculation/{SigID}</Pattern>
</URIPath>

 Then your could apply that same policy in each of the respective flows.

Maybe that's not what your trying to do, but perhaps it may help.

You can't do just what you're describing with the ExtractVariables policy, though I thnk what you're asking for is a nifty idea for a small enhancement. 

If you really need the original pattern I would use JavaScript and just iterate through the patterns.  Maybe something like this: 

var patterns = {
        checkout: '/user/details-list/{ID}/checkout',
        balance: '/user/details-list/{ID}/balance',
        stop: '/user/details-list/{CID}/checkout/stop-calculation/{SigID}',
        profile: '/user/{COID}/profile'
      };

var re1 = new RegExp('\{([^}]+)\}', 'g');

function match(path) {
  return Object.keys(patterns)
    .map(function(key) {
      var pattern = patterns[key];
      var extractedNames = [];
      var m1;
      while ((m1 = re1.exec(pattern)) != null) {
        extractedNames.push(m1[1]);
      }
      var re = new RegExp('^' + pattern.replace(re1, '([^/]+)') + '$');
      var m = re.exec(path);
      if (m) {
        var obj = { patternName:key, pattern:pattern, extracts:{} };
        extractedNames.forEach(function(name, ix){
          obj.extracts[name] = m[1 + ix];
        });
        return obj;
      }
    })
    .find(function(value) { return value; });
}

var m = match(context.getVariable('proxy.pathsuffix'));
if (m) {
 // eg
 // { "patternName":"stop",
 //   "pattern":"/user/details-list/{CID}/checkout/stop-calculation/{SigID}",
 //   "names":{"CID":"567","SigID":"abcchdk"}
 // }
 context.setVariable('patternName', m.patternName);
 context.setVariable('pattern', m.pattern);
 Object.keys(m.names).forEach(function(fieldName) {
    context.setVariable('extracted.' + fieldName, m.names[fieldName]); 
 });
}