extracting URIPATH suffix

i have configure below policy and trying to pass URI and query params to backend by removing 1st string after base path i.e tmdd

https://domain.com/postux/tmdd/api/transactions/original-payments/?companyCode=TMW&brandCode=TMW&res...

target endpoint will be https://targetsystem.com/api/transactions/original-payments/?companyCode=TMW&brandCode=TMW&reservati...

i have something like this, trying to remove {brand} before passing to backend.

<ExtractVariables continueOnError="false" enabled="true" name="Extract-Brand">
  <DisplayName>Extract-Brand</DisplayName>
  <Source>request</Source>
  <URIPath>
    <Pattern ignoreCase="true">/{brand}/api/transactions/**</Pattern>
  </URIPath>
  <VariablePrefix>urirequest</VariablePrefix>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</ExtractVariables>
 
THanks,
Raj
Solved Solved
0 4 258
1 ACCEPTED SOLUTION

Thank you. QueryParam is not getting added to target.url. I updated like below. please correct me if this solution is wrong.

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-Adjust-Path">
  <DisplayName>AM-Adjust-Path</DisplayName>
  <!-- replace the path segment including the leading slash -->
  <AssignVariable>
    <Name>replacebrand</Name>
    <Template>/{urirequest.brand}</Template>
  </AssignVariable>
  <!-- replace the thing-to-replace with the empty string -->
  <AssignVariable>
    <Name>target.url</Name>
    <Template>{target.url}{replaceFirst(proxy.pathsuffix,replacebrand,'')}?{request.querystring}</Template>
  </AssignVariable>
  <!-- propagate the brand to the upstream -->
  <!-- <Set>
       <Headers>
       <Header name='extracted-brand'>{urirequest.extracted-segment}</Header>
       </Headers>
       </Set> -->
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
  <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

View solution in original post

4 REPLIES 4

Hey Raj

ExtractVariables does not modify URIPaths or JSON Payloads or anything having to do with the message.  ExtractVariables just extracts values from those things, and inserts those values into newly created context variables. If you want to extract the segment from the URI Path and insert it into a variable , you can use ExtractVariables to do that. I think you had a separate question on this yesterday, and the example policy you showed above, does that: it extracts the path segment. 

If you want to modify the message, including modifying the URI Path, for that you need to use either AssignMessage (Set/Path), or a JavaScript callout that directly manipulates the target.url variable. 

In either case you need to attach that policy to the Target Request flow, to make that change.  In AssignMessage it might look like this: 

 

<AssignMessage name='AM-Adjust-Path'>
  <!-- we want to replace the path segment including the leading slash -->
  <AssignVariable>
    <Name>thing-to-replace</Name>
    <Template>/{urirequest.extracted-segment}</Template>
  </AssignVariable>
  <!-- replace the thing-to-replace with the empty string -->
  <AssignVariable>
    <Name>target.url</Name>
    <Template>{target.url}{replaceFirst(proxy.pathsuffix,thing-to-replace,'')}</Template>
  </AssignVariable>
  <!-- if you want to propagate the brand to the upstream -->
  <Set>
    <Headers>
      <Header name='extracted-brand'>{urirequest.extracted-segment}</Header>
    </Headers>
  </Set>
</AssignMessage>

 

 

 

Thank you. QueryParam is not getting added to target.url. I updated like below. please correct me if this solution is wrong.

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-Adjust-Path">
  <DisplayName>AM-Adjust-Path</DisplayName>
  <!-- replace the path segment including the leading slash -->
  <AssignVariable>
    <Name>replacebrand</Name>
    <Template>/{urirequest.brand}</Template>
  </AssignVariable>
  <!-- replace the thing-to-replace with the empty string -->
  <AssignVariable>
    <Name>target.url</Name>
    <Template>{target.url}{replaceFirst(proxy.pathsuffix,replacebrand,'')}?{request.querystring}</Template>
  </AssignVariable>
  <!-- propagate the brand to the upstream -->
  <!-- <Set>
       <Headers>
       <Header name='extracted-brand'>{urirequest.extracted-segment}</Header>
       </Headers>
       </Set> -->
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
  <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

Oh, yes, modify it that way to add the querystring. That works for me. 

Also, an AssignTo element with no text value, is meaningless. Omit it. 

 

<AssignMessage continueOnError="false" enabled="true" name="AM-Adjust-Path">
  ...
  <!-- the following is meaningless -->
  <AssignTo createNew="false" transport="http" type="request"/>

</AssignMessage>

 

 

Thank you for solution 🙂