David Gard
David Gard

Reputation: 12087

Work with URL template parameter values in policy templates

In API Management, how do you access URL template parameters through a policy?

In this instance, my operation is called test, the HTML verb is GET, and the URL template is as below -

/test/{variable_name1}/{variable_name2}

I was under the impression that accessing the value of a parameter was as simple as {variable_name1}. However, the example below does not set the variable "rowkey" as expected. Rather it has a value of {variable_name1}-{variable_name2}.

What am I doing wrong here?

<policies>
    <inbound>
        <set-variable name="rowkey" value="{variable_name1}-{variable_name2}" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <set-header name="Row-Key" exists-action="override">
            <value>@((string)context.Variables["rowkey"])</value>
        </set-header>
    </outbound>
</policies>

Upvotes: 5

Views: 9476

Answers (1)

Vitaliy Kurokhtin
Vitaliy Kurokhtin

Reputation: 7840

You'd have to use expressions to achieve what you want, like:

<set-variable 
   name="rowkey" 
   value="@(context.Request.MatchedParameters["variable_name1"] + "-" + context.Request.MatchedParameters["variable_name2"])" />

or use string interpolation:

<set-variable 
   name="rowkey" 
   value="@($"{context.Request.MatchedParameters["variable_name1"]}-{context.Request.MatchedParameters["variable_name2"]}")" />

Upvotes: 11

Related Questions