如何在 Azure API 管理中为查询字符串参数指定重写 url

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

我正在使用 Azure API 管理将传入的查询字符串转换为另一个查询字符串。

我的转换代码是:

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

当我尝试保存编辑时,出现错误:

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

指的是a={a}中的等号。如何更正 rewrite-uri 的模板?输入 url 例如 https://example.com/sum?a=7&b=5.

尝试替换:

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

与:

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

https://azure.microsoft.com/en-us/blog/policy-expressions-in-azure-api-management/ 找到更多详细信息。

您只需在 APIM 中创建“查询参数”而不是 "Template parameters"。 然后,您的重写 uri 不需要包含查询参数,因为 APIM 会在通过入站提供后自动将其添加到后端 url。

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

如果请求 URL 是这样的:

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

那么发送到后端的 HTTP 请求将是这样的:

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

如果请求 URL 没有这样的查询字符串:

https://example.com/sum

那么发送到后端的 HTTP 请求就是这样:

GET backendapi/api/primes