how to read value of wildcard from proxy basepath

Not applicable

Hi There,

I was playing around with wildcard feature in proxy basepath. I can successfully makes call with wildcards in proxy basepath but couldn't workout how to extract value of wildcard in some variable. even if I have multiple wildcards.

I tried using Extract variable policy but seems like it only works for pathsuffix not basepath.

any idea?

Thanks

Harmeet

Solved Solved
1 5 2,549
1 ACCEPTED SOLUTION

Not applicable

Good catch @harmeet.sra12 . You're right! I've just tried parsing the basepath and I realized that ExtractVariables policy (URIPath element specifically) doesn't support including the basepath. In fact, to search you should always remove the basepath from the search expression. However, there are alternatives to deal with this. I've just published an API Proxy demonstrating how to use a JavaScript policy to parse message.uri, which is the variable that contains the path along with query params. Essentially, all you'll need is something along these lines:

var pathArray = context.getVariable("message.uri").split("/");
context.setVariable( "firstPathElement", pathArray[2] );
context.setVariable( "secondPathElement", pathArray[4] );

For more step by step details, check out this fully functional API Proxy, ready to be deployed to you free org. https://github.com/dzuluaga/apigee-tutorials/tree/master/apiproxies/parse-basepath-with-policies.

=====

For the impatient, here's a copy of the content of the API Proxy:

how-to-parse-basepath-wildcard-with-policies

Currently ExtractVariables policy (URIPath element specifically) can only parse the path suffix. Here's an altenative to parse the basepath with JavaScript:

how to retrieve variables

how to parse with JS policies?

var pathArray = context.getVariable("message.uri").split("/");
context.setVariable( "firstPathElement", pathArray[2] );
context.setVariable( "secondPathElement", pathArray[4] );

how to deploy this api proxy?

apigeetool deployproxy  -u $ae_username -p $ae_password -o testmyapi -e test -n parse-basepath-with-policies -d . -V

how to test it?

curl https://testmyapi-test.apigee.net/parse-basepath-with-policies/12345/test/111111 -v

Check variables populated in Trace tool on Apigee.

View solution in original post

5 REPLIES 5

Not applicable

Good catch @harmeet.sra12 . You're right! I've just tried parsing the basepath and I realized that ExtractVariables policy (URIPath element specifically) doesn't support including the basepath. In fact, to search you should always remove the basepath from the search expression. However, there are alternatives to deal with this. I've just published an API Proxy demonstrating how to use a JavaScript policy to parse message.uri, which is the variable that contains the path along with query params. Essentially, all you'll need is something along these lines:

var pathArray = context.getVariable("message.uri").split("/");
context.setVariable( "firstPathElement", pathArray[2] );
context.setVariable( "secondPathElement", pathArray[4] );

For more step by step details, check out this fully functional API Proxy, ready to be deployed to you free org. https://github.com/dzuluaga/apigee-tutorials/tree/master/apiproxies/parse-basepath-with-policies.

=====

For the impatient, here's a copy of the content of the API Proxy:

how-to-parse-basepath-wildcard-with-policies

Currently ExtractVariables policy (URIPath element specifically) can only parse the path suffix. Here's an altenative to parse the basepath with JavaScript:

how to retrieve variables

how to parse with JS policies?

var pathArray = context.getVariable("message.uri").split("/");
context.setVariable( "firstPathElement", pathArray[2] );
context.setVariable( "secondPathElement", pathArray[4] );

how to deploy this api proxy?

apigeetool deployproxy  -u $ae_username -p $ae_password -o testmyapi -e test -n parse-basepath-with-policies -d . -V

how to test it?

curl https://testmyapi-test.apigee.net/parse-basepath-with-policies/12345/test/111111 -v

Check variables populated in Trace tool on Apigee.

Thanks Diego,

yes Javascript is an option to do it. It would have been nice to extend extract variable policy to support this.

Thanks

Harmeet

Just stumbled across your question @harmeet.sra

You can use Extract Variables and extract the wildcard value from the basepath using the variable "proxy.basepath".

Given you have a wildcard basepath like "/api/*/*"

And you did GET /api/v1/orders/616646/addresses

Then you could use:

<ExtractVariables async="false" continueOnError="false" enabled="true" name="EV-BasepathAndSuffixes">
    <DisplayName>EV-BasepathAndSuffixes</DisplayName>
    <Properties/>
    <Variable name="proxy.basepath">
        <Pattern>/api/{basepath1}/{basepath2}</Pattern>
    </Variable>
    <URIPath>
        <Pattern ignoreCase="false">/{suffix1}/{suffix2}</Pattern>
    </URIPath>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <Source clearPayload="false">request</Source>
</ExtractVariables>

Then the values extracted would be:

basepath1=v1

basepath2=orders

suffix1=616646

suffix2=addresses

Better late than never I guess.

Using proxy.basepath for your variable name will not always work. This value is always the exact value that you have specified in your proxy. So if you have wildcard in your path, /api/v1/*, then that is the value that will be parsed in ExtractVariables.

To be able to parse the exact path, there are a few variables you can use. I happened to use

request.path

but I suspect that there are a few others that would work also.

in my case wildcard basepath is "/*/v1/xyz", as @tomvandegrift rightly mentioned; for me "request.path" works, but "proxy.basepath" does not.