How can we extract dynamic parts from the URI path ?

We have the base path for a Proxy which has some dynamic parts (wild card characters) in it:

<BasePath>/dev/v1/ships/*/sailDate/*/enhanced</BasePath>

Let’s say we make a API call with the URL as follows:

curl -v https://org-env.apigee.net/dev/v1/ships/10045/sailDate/22Sep/enhanced

We would like to extract the values passed to the dynamic variables (wild card characters) of the base path/URI and use in a Service Callout Policy. We created the following Extract Variables Policy as follows to extract the values from the URI:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-URI-Variables"> 
   <DisplayName>Extract URI Variables</DisplayName> 
   <URIPath name="proxy.url"> 
      <Pattern ignoreCase="true">/ships/{shipCode}/sailDate/{sailDate}</Pattern>
   </URIPath> 
</ExtractVariables> 

And then we tried to use the flow variables shipCode and sailDate in the Service Callout policy

======== Service-CallOut ========

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="false" continueOnError="false" enabled="true" name="Service-CallOut">
  <DisplayName>Service CallOut</DisplayName>
  <Properties/>
  <Request clearPayload="true" variable="myRequest">
    <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
  </Request>
  <Response>calloutResponse</Response>
  <HTTPTargetConnection>
  <URL>http://mybackendserver/v01/ports/ships/{shipCode}/sailDate/{sailDate}</URL>
  </HTTPTargetConnection>
</ServiceCallout>

But I'm getting the following error:

{
  "fault": {
    "faultstring": "Unresolved variable : shipCode",
    "detail": {
      "errorcode": "messaging.runtime.UnresolvedVariable"
    }
  }
}

Can you please help on why am I getting this error and also help me to get the values of shipCode and sailDate ?

2 3 1,833
3 REPLIES 3

Hi Amar

I think you just have a mismatch between the inbound URI and the pattern. The inbound seems to end in "/enhanced", but the pattern does not.

So I think you need this pattern:

<ExtractVariables name="EV-GetUriPathElements">
   <DisplayName>Extract URI Variables</DisplayName>
   <URIPath name="proxy.url">
      <Pattern ignoreCase="true">/ships/{shipCode}/sailDate/{sailDate}/enhanced</Pattern>
   </URIPath>
</ExtractVariables>

If you want to make this work whether /enhanced is there or not, then do this:

<ExtractVariables name="EV-GetUriPathElements">
   <DisplayName>Extract URI Variables</DisplayName>
   <URIPath name="proxy.url">
      <Pattern ignoreCase="true">/ships/{shipCode}/sailDate/{sailDate}</Pattern>
      <Pattern ignoreCase="true">/ships/{shipCode}/sailDate/{sailDate}/enhanced</Pattern>
   </URIPath>
</ExtractVariables>

Cheers!

@Dino,

Thanks for your suggestion. I tried this and also had actually tried multiple other ways, but nothing helped.

@Dino

I went through the Extract Variables policy documentation and realised that we can extract the values from the path that exists after the basepath.

For example,

If the basepath of my API Proxy is /dev/v1

and my request URI is /dev/v1/ships/10045/sailDate/22Sep/enhanced

then I was able to extract the values using the Extract Variables policy as follows:

shipCode 10045
sailDate 22Sep

However, I could not extract the values using the Extract Variables policy if the dynamic variables are a part of the basepath itself.

I found another way to achieve this using the JavaScript policy as follows:

var shipCodeIndex = 4;
var sailDateIndex = 6;
var pathArray = context.getVariable("message.uri").split("/");
context.setVariable( "shipCode", pathArray[shipCodeIndex] );
context.setVariable( "sailDate", pathArray[sailDateIndex] );

I got the following output:

message.uri /dev/v1/ships/10045/sailDate/22Sep/enhanced
sailDate 22Sep
shipCode 10045

Not sure if this is the best way to achieve this.

If there are any other better ways to do this, please let me know.

Thanks,

Amar