代码之家  ›  专栏  ›  技术社区  ›  Phillip Ngan

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

  •  1
  • Phillip Ngan  · 技术社区  · 7 年前

    我正在使用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 回复  |  直到 7 年前
        1
  •  3
  •   Evandro de Paula    7 年前

    尝试替换:

    <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/ 是的。

        2
  •  0
  •   Amir Chatrbahr    7 年前

    你只需要创建“ 查询参数 而不是“模板参数”。 然后,重写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