【问题标题】:Rename Query Parameter in Outbound Request in Azure API Management在 Azure API 管理的出站请求中重命名查询参数
【发布时间】:2020-05-15 15:44:44
【问题描述】:

使用 Azure API 管理中的策略,我正在尝试重命名查询参数,但它不起作用。如果我将 copy-unmatched-params="false" 更改为 copy-unmatched-params="true" 那么它可以工作,但是行为变成所有不匹配的参数都将传递给后端 API,这将允许客户端注入他们自己的查询将参数放入我们不想要的后端请求中。

其他一切都很好。

我想转换一个如下所示的请求:

https://{site}/search?query=dylan

收件人:

https://{backend-site}documents?api-version=2018-1-11&amount=1000&searchFields=Album,Artist&search=dylan

唯一不起作用的部分是将查询参数转换为名为“搜索”而不是查询,而不允许从入站查询字符串传递所有参数。我该如何解决?

<policies>
    <inbound>
        <rewrite-uri template="/" copy-unmatched-params="false" />
        <set-header name="api-key" exists-action="override">
            <value>THIS-IS-API-KEI</value>
        </set-header>
        <set-query-parameter name="api-version" exists-action="override">
            <value>2018-1-11</value>
        </set-query-parameter>
        <set-query-parameter name="amount" exists-action="override">
            <value>1000</value>
        </set-query-parameter>
        <set-query-parameter name="searchFields" exists-action="override">
            <value>Album,Artist</value>
        </set-query-parameter>
        <set-query-parameter name="search" exists-action="override">
            <value>@(context.Request.Url.Query.GetValueOrDefault("query"))</value>
        </set-query-parameter>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

【问题讨论】:

    标签: azure azure-api-management


    【解决方案1】:

    您从上一个表达式中获得空值的原因是,到那时您的 URI 已经重写为“/”并且只设置了“api-version”、“amount”和“searchFields”查询参数。有几种方法可以解决这个问题:

    1. 参考原网址:@(context.Request.OriginalUrl.Query.GetValueOrDefault("query"))
    2. 将查询添加到操作 URI 模板 - search?query={query} - 并从 rewrite-uri 策略引用它:&lt;rewrite-uri template="/?query={query}" copy-unmatched-params="false" /&gt;。缺点是需要“查询”参数,因此任何没有它的请求都会导致 404。

    【讨论】:

      【解决方案2】:

      尝试使用变量。在开始时将值赋给变量,并使用该变量来分配新的查询参数

      【讨论】:

        【解决方案3】:

        您可以使用 URL 的字符串和简单的替换方法来重命名查询中的参数。在这种情况下,该参数不是强制性的。

        <inbound>
             <base />
             <rewrite-uri template="@{
                return "/some-url-here-or-your-previously-constructed-url" + context.Request.OriginalUrl.QueryString
                    .Replace("old-name", "new-name");
                 }" copy-unmatched-params="false" />
         </inbound>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-06-03
          • 2018-02-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多