Extract variable url use javascript

Not applicable

I have and endpoint like

GET /cars/22/images

I want to add a policy to extract 22. I created this

<ExtractVariables name="ExtractCarId-1">
    <Source>request</Source>
    <URIPath>
        <Pattern ignoreCase="true">/cars/{carId}/images</Pattern>
    </URIPath>
    <VariablePrefix>urirequest</VariablePrefix>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</ExtractVariables>

and associated to the request flow.

I now want to access the extracted value from a JS policy on the `response` flow. But if I write "urirequest.carId" I get a 500 with the error

ReferenceError: \"urirequest\" is not defined

What am I doing wrong ?

Solved Solved
0 2 2,370
1 ACCEPTED SOLUTION

hi @Simone Fumagalli interesting to read variable scope - http://docs.apigee.com/api-services/reference/variables-reference.

IMO, scope of extracted variable in your policy above is in request flow, so it wont be available to response until unless you save it in context may be using JavaScript policy right after extraction policy;

...
var carId = context.getVariable('urirequest.carId');

context.setVariable('carId',carId);
...

View solution in original post

2 REPLIES 2

hi @Simone Fumagalli interesting to read variable scope - http://docs.apigee.com/api-services/reference/variables-reference.

IMO, scope of extracted variable in your policy above is in request flow, so it wont be available to response until unless you save it in context may be using JavaScript policy right after extraction policy;

...
var carId = context.getVariable('urirequest.carId');

context.setVariable('carId',carId);
...

Great !

For the record. To read the variable in the JS policy in response flow you can use:

carIdInResponse = context.getVariable('carId');