Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Policy rewrite-uri To Append Context Variable in Azure APIM

What is the approach to a simple append to the url of a context variable such as context.Variables["accountKey"] during a policy rewrite?

The end result should be /accounts/232.

I have success earlier in setting it

set-variable (0.003 ms)
{
    "message": "Context variable was successfully set.",
    "name": "accountKey",
    "value": "232"
}

One of the things tried:

<policies>
    <inbound>
        <base />
        <rewrite-uri template="/accounts/{accountKey}" />
    </inbound>

But I get this error

> Error Receive
>     rewrite-uri (0.260 ms) {
>     "messages": [
>         null,
>         "Variable accountKey has no value.",
>         "Variable accountKey has no value."
>     ] }
like image 637
ΩmegaMan Avatar asked May 18 '18 16:05

ΩmegaMan


People also ask

How do I change base URL in Apim?

There is no way to change gateway URL i.e, Base URL through CLI or any other place. When we create Azure APIM by default it takes(base url: <apim-service-name>. azure-api.net , Developer url: <apim-service-name>.

What is the conditional policy in Apim?

The Azure API Management Portal allows API Publishers to set policies to change the behavior of the underlying API by configuration. The Policies act like a pipeline that executes a set of conditions or rules in a sequence.

Which file format is used for Azure API gateway policies?

Policies are defined in XML documents that contain a sequence of statements that are run sequentially on the request or response of an API.


1 Answers

Configure the inbound rule in the policy as follows:

<inbound>
    <base />
    <set-variable name="accountKey" value="232" />
    <rewrite-uri template="@{
        return "/account/" + context.Variables.GetValueOrDefault<string>("accountKey");
    }"/>
</inbound>

{} in rewrite-uri are for query string parameters in the original request URL.

Find more details about rewrite-uri section in the Microsoft docs at Rewrite URL - API Management transformation policies.

like image 160
Evandro de Paula Avatar answered Oct 21 '22 19:10

Evandro de Paula