Phillip Ngan
Phillip Ngan

Reputation: 16126

How to specify rewrite url for query string parameters in Azure API Management

I'm using the Azure API Management to transform the incoming query string into another query string.

My transformation code is:

<policies>
    <inbound>
        <rewrite-uri template="api/primes?a={a}&b={b}" />
        <base />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

When I try to save the edits, the error appears:

One or more fields contain incorrect values: 
'=' is an unexpected token. The expected token is ';'. Line 15, position 50.

which refers to the equals symbol as in a={a}. How do I correct the template of the rewrite-uri? The input url is for example https://example.com/sum?a=7&b=5.

Upvotes: 6

Views: 14822

Answers (3)

codeMonkey
codeMonkey

Reputation: 4845

In my case, incoming query params were being encoded by APIM, so I had to do the following to get a 1:1:

<inbound>
    <base />
    <set-variable name="queryDecoded" value="@(System.Net.WebUtility.UrlDecode(context.Request.Url.QueryString))" />
    <rewrite-uri template="@{
        var queryDecoded = (string)context.Variables["queryDecoded"];
        var uri = "/path" + queryDecoded;
        return uri;        
    }" copy-unmatched-params="false" />
</inbound>

Upvotes: 0

Amir Chatrbahr
Amir Chatrbahr

Reputation: 2370

You only need to create "Query Parameters" in APIM instead of "Template parameters". Then your rewrite uri doesn't need to include the Query parameters as APIM will add it to backend url automatically once it is provided via inbound.

<rewrite-uri template="api/primes" />

if request URL is something like this:

https://example.com/sum?a=7&b=5

then the HTTP request sent to backend would be like this:

GET backendapi/api/primes?a=7&b=5

and if request URL is without query strings like this:

https://example.com/sum

then the HTTP request sent to backend would be simply like this:

GET backendapi/api/primes

Upvotes: 0

Evandro de Paula
Evandro de Paula

Reputation: 2642

Try replacing:

<rewrite-uri template="api/primes?a={a}&b={b}" />

With:

<rewrite-uri template="api/primes?a={a}&amp;b={b}" />

Find more details at https://azure.microsoft.com/en-us/blog/policy-expressions-in-azure-api-management/.

Upvotes: 10

Related Questions