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

3

6

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.

Armagh answered 9/7, 2018 at 21:17 Comment(0)
M
10

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/.

Mobile answered 9/7, 2018 at 21:33 Comment(0)
R
0

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
Reduplicate answered 20/9, 2018 at 7:14 Comment(1)
This may have worked before, but this no longer works for me. For instance the value 2024-10-02T23%3A37%3A34Z is encoded again as 2024-10-02T23%253A37%253A34Z by APIM, which is causing the connection to fail.Estovers
E
0

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>
Estovers answered 3/10, 2024 at 6:13 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.