priority order in extract variable policy

Not applicable

I want to create extract variable policy such that variable is extracted either from queryparam or header. If have both of the following in my policy which one will take precedence

<QueryParam name="apikey"> <Pattern ignoreCase="true">{apikey}</Pattern> </QueryParam> ... <Header name="Authorization"> <Pattern ignoreCase="true">myapi-apikey key={apiKey}</Pattern></Header>

Solved Solved
2 3 361
1 ACCEPTED SOLUTION

Hi @Gagan Arora

To the best of my knowledge the Policy will be executed sequentially in the order in which it is defined . So in your example since the query param is before the header's definition the variable will be extracted from QueryParam.

But after that the apikey variable will also be extracted from header and it will override the previously set value.

Now if you don't want that to happen what I have done in the past is to break this into two different policies . One policy extracting from query param and the other from header . And you attach a condition while executing the second Extract Variables policy. If Apikey is null then execute the 2nd AssignMessage else don't.

Hope this helps.

Thanks

Sarthak

View solution in original post

3 REPLIES 3

Hi @Gagan Arora

To the best of my knowledge the Policy will be executed sequentially in the order in which it is defined . So in your example since the query param is before the header's definition the variable will be extracted from QueryParam.

But after that the apikey variable will also be extracted from header and it will override the previously set value.

Now if you don't want that to happen what I have done in the past is to break this into two different policies . One policy extracting from query param and the other from header . And you attach a condition while executing the second Extract Variables policy. If Apikey is null then execute the 2nd AssignMessage else don't.

Hope this helps.

Thanks

Sarthak

Sarthak is correct: the ExtractVariables policy is executed in order from top to bottom. Whichever you want to take precedence when both are specified should be extracted last. Be sure to comment in your ExtractVariables policy so that developers know what is going on:

<!-- if apiKey found in both query param and header, header wins -->
<QueryParam name="apikey">
  <Pattern ignoreCase="true">{apiKey}</Pattern>
</QueryParam>

<!-- overwrite apiKey even if it was found in query parameter -->
<Header name="Authorization">
  <Pattern ignoreCase="true">myapi-apikey key={apiKey}</Pattern>
</Header>

Not applicable

Last in wins - the policy runs top to bottom and will actually extract the variable wherever it matches with the last value extracted being the one that persists after the policy execution.

This is distinct from how flows are executed in proxy definitions - in that case the order is top to bottom but one and only one flow is executed - that being the first one that matches the condition criteria.