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

时间:2018-07-09 21:17:38

标签: azure 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

2 个答案:

答案 0 :(得分:3)

尝试更换:

<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/上找到更多详细信息。

答案 1 :(得分:0)

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

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

如果请求网址是这样的:

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