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."
> ] }
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>.
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.
Policies are defined in XML documents that contain a sequence of statements that are run sequentially on the request or response of an API.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With