繁体   English   中英

Azure API 管理策略(重写URI)-模板参数和查询参数

[英]Azure API Management Policy(Rewrite URI)- Template Paramets and Query Parameters

嗨,我仍在了解有关 Azure API 管理(APIM)的所有信息

我需要一些帮助来执行重写 uri 策略。 我能够从有效负载中提取信息并更改初始请求 URL。 从正文中提取了insurer_id 并将其传递给我的后端-> mybackend.com/api/relationship/deny/{insurer_id}

我有一个当前的有效载荷:

{
  "insurer_id": "22112",
  "insurer_name": "Steve Rogers",
  "status_code: [ "Deny",
  ],
  "additionalComments": "This is a test"
}

我能够按照下面的代码做到这一点。

但我还希望能够从我的有效负载中提取另一个密钥,即“status_code”并将其作为后端的一部分传递,并更改初始请求 URL

例如: https://mybackend.com/api/relationship/deny/{insurer_id}?denyReason={status_code}变成https://mybackend.com/api/relationship/deny/221112

这是当前遇到错误的代码的尝试:

<policies>
    <inbound>
        <base />
        <set-variable name="insurerId" value="@{
            var body = context.Request.Body.As<JObject>(true);
            return body["insurer_id"].Value<string>();
        }" />
    
<set-variable name="status_code" value="@{
            var body = context.Request.Body.As<JObject>(true);
            return body["status_code"].Value<string>();
        }" />
        <rewrite-uri template="@("/api/relationship/deny/" + context.Variables.GetValueOrDefault<string>("insurer_id")+ ? + denyReason=context.Variables.GetValueOrDefault<string>("status_code")") copy-unmatched-params="false" />
        <set-backend-service base-url="https://testbackend.azure-api.net" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

对此的任何帮助将不胜感激。

谢谢你。

一个查询字符串由一个键值对组成:

https://example.com/over/there?name=ferret

name是关键
ferret是它的价值

在您的示例中,没有密钥:

? + denyReason=context.Variables.GetValueOrDefault<string>("status_code")
还有? 是一个字符串。 所以它必须放在引号之间。

使用示例查询键status_code修复:

<rewrite-uri template="@("/api/relationship/deny/" + context.Variables.GetValueOrDefault<string>("insurerId") + "?statuscode=" + context.Variables.GetValueOrDefault<string>("status_code"))" copy-unmatched-params="false" />

完整的政策:

<policies>
    <inbound>
        <base />
        <set-variable name="insurerId" value="@{
            var body = context.Request.Body.As<JObject>(true);
            return body["insurer_id"].Value<string>();
        }" />
        <set-variable name="status_code" value="@{
                var body = context.Request.Body.As<JObject>(true);
                return body["status_code"].Value<string>();
         }" />
        <rewrite-uri template="@("/api/relationship/deny/" + context.Variables.GetValueOrDefault<string>("insurerId") + "?statuscode=" + context.Variables.GetValueOrDefault<string>("status_code"))" copy-unmatched-params="false" />
        <set-backend-service base-url="https://testbackend.azure-api.net" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM