2

注意:请参阅下面的我自己的答案

我已经浏览了几个小时的纤薄文档。我需要的是这个:

我有一个来自只能发送 GET 请求的系统的传入 GET 请求。我的后端服务需要 POST 请求。

关于我应该使用什么 APIM 策略或策略组合,有人能指出我正确的方向吗?

我正在使用策略参考,但仍然无法提出一个体面的解决方案: https ://msdn.microsoft.com/en-us/library/azure/dn894081.aspx

4

3 回答 3

4

我找到了解决方案。

这是场景:我需要客户端调用 APIM 端点并且只得到简单的响应。即....认证=真/假。

我有一个来自只能发送 GET 请求的系统的传入 GET 请求。我的后端服务需要 POST 请求。

这是我来自只能发送 GET 请求的客户端的 GET 调用:https:///api/v1//10010810?pin=1212&property=Sydney&subscription-key=XXXXXXXXXXXXXXX&debugging=1

这是我的政策:

<policies>
<inbound>
    <set-header name="Content-Type" exists-action="override">
        <value>application/json</value>
    </set-header>
    <trace source="defaultTrace">
        @{
            return System.String.Format("The passed in querystring paramters were | pin: {0} | debugging: {1} | property: {2} | subscription-key: {3}", 
                context.Request.Url.Query.GetValueOrDefault("pin"), 
                context.Request.Url.Query.GetValueOrDefault("debugging"),
                context.Request.Url.Query.GetValueOrDefault("property"),
                context.Request.Url.Query.GetValueOrDefault("subscription-key")
                );
        }
    </trace>
    <set-variable name="requestPin" value="@(context.Request.Url.Query.GetValueOrDefault("pin"))" />
    <set-variable name="debugging" value="@(context.Request.Url.Query.GetValueOrDefault("debugging"))" />
    <trace source="defaultTrace">
        @{
            return System.String.Format(
            "Removing the following querystring parameters from url as we don't want to pass these ones to the backend service debugging: {0} | subscription-key: {1} | pin: {2}", 
                context.Request.Url.Query.GetValueOrDefault("debugging"),
                context.Request.Url.Query.GetValueOrDefault("subscription-key"),
                context.Request.Url.Query.GetValueOrDefault("pin")
                );
        }
    </trace>
    <set-query-parameter name="subscription-key" exists-action="delete" />
    <set-query-parameter name="debugging" exists-action="delete" />
    <set-query-parameter name="pin" exists-action="delete" />
    <base />
</inbound>
<backend>
    <!-- Setup a response-variable-name to hold the response from the backend service-->
    <send-request mode="copy" response-variable-name="microservice-response" timeout="20" ignore-error="false">
        <!-- Set the method to POST as the backend service MUST receive a POST call-->
        <set-method>POST</set-method>
        <set-body>
            @{
                // Get the pin from the url as we need it to construct the POST body
                var requestPin = context.Variables.GetValueOrDefault<string>("requestPin");

                var postBody = new JObject(
                    new JProperty("Type", "Pin"),
                    new JProperty("Value", requestPin)
                ).ToString();
                return postBody;
            }
        </set-body>
    </send-request>
</backend>
<outbound>
    <choose>
        <when condition="@(((IResponse)context.Variables["microservice-response"]).StatusCode == 200)">
            <!-- When the micro-service returned a valid response we put the response into the previously created variable called  microservice-response -->
            <return-response>
                <set-status code="200" reason="Ok" />
                <set-body>
                    @{
                        var debuggingVariable = context.Variables.GetValueOrDefault<string>("debugging");
                        var microserviceResponse = ((IResponse)context.Variables["microservice-response"]).Body.As<JObject>();

                        if(debuggingVariable == "1")
                        {
                            var returnResponse = new JObject(
                                new JProperty("Authenticated", true),
                                new JProperty("MicroserviceResponse", microserviceResponse),
                                new JProperty("StatusCode", ((IResponse)context.Variables["microservice-response"]).StatusCode)
                            ).ToString();
                            return returnResponse.ToString();
                        }
                        else
                        {
                            var returnResponse = new JObject(new JProperty("Authenticated", true)).ToString();
                            return returnResponse.ToString();
                        }
                    }
                </set-body>
            </return-response>
        </when>
        <when condition="@(((IResponse)context.Variables["microservice-response"]).StatusCode == 401)">
            <!-- When the micro-service returned a valid response we put the response into the previously created variable called  microservice-response -->
            <return-response>
                <set-status code="401" reason="Error" />
                <set-body>
                    @{
                        var debuggingVariable = context.Variables.GetValueOrDefault<string>("debugging");
                        var microserviceResponse = ((IResponse)context.Variables["microservice-response"]);

                        if(debuggingVariable == "1")
                        {
                            var returnResponse = new JObject(
                                new JProperty("Authenticated", false),
                                new JProperty("MicroserviceResponse", microserviceResponse.Body.As<JObject>().ToString()),
                                new JProperty("StatusCode", ((IResponse)context.Variables["microservice-response"]).StatusCode)
                            ).ToString();
                            return returnResponse.ToString();
                        }
                        else
                        {
                            var returnResponse = new JObject(new JProperty("Authenticated", false)).ToString();
                            return returnResponse.ToString();
                        }
                    }
                </set-body>
            </return-response>
        </when>
        <when condition="@(((IResponse)context.Variables["microservice-response"]).StatusCode == 400)">
            <!-- When the micro-service returned a valid response we put the response into the previously created variable called  microservice-response -->
            <return-response>
                <set-status code="200" reason="Ok" />
                <set-body>
                    @{
                        var debuggingVariable = context.Variables.GetValueOrDefault<string>("debugging");
                        var microserviceResponse = ((IResponse)context.Variables["microservice-response"]);

                        if(debuggingVariable == "1")
                        {
                            var returnResponse = new JObject(
                                new JProperty("Authenticated", false),
                                new JProperty("MicroserviceResponse", microserviceResponse.Body.As<JObject>().ToString()),
                                new JProperty("StatusCode", ((IResponse)context.Variables["microservice-response"]).StatusCode)
                            ).ToString();
                            return returnResponse.ToString();
                        }
                        else
                        {
                            var returnResponse = new JObject(new JProperty("Authenticated", false)).ToString();
                            return returnResponse.ToString();
                        }
                    }
                </set-body>
            </return-response>
        </when>
        <otherwise>
            <return-response>
                <!-- When the micro-service threw an exception we just want to show the caller Authenticated = false -->
                <!--<set-status code="500" reason="Error" />-->
                <set-body>
                        @{
                            var debuggingVariable = context.Variables.GetValueOrDefault<string>("debugging");
                            var microserviceResponse = ((IResponse)context.Variables["microservice-response"]);

                            if(debuggingVariable == "1")
                            {
                                var returnResponse = new JObject(
                                    new JProperty("Authenticated", false),
                                    new JProperty("MicroserviceResponse", microserviceResponse.Body.As<JObject>().ToString()),
                                    new JProperty("StatusCode", ((IResponse)context.Variables["microservice-response"]).StatusCode)
                                ).ToString();
                                return returnResponse.ToString();
                            }
                            else
                            {
                                var returnResponse = new JObject(new JProperty("Authenticated", false)).ToString();
                                return returnResponse.ToString();
                            }
                        }
                </set-body>
            </return-response>
        </otherwise>
    </choose>
    <base />
</outbound>
<on-error>
    <!-- When APIM threw an exception -->
    <trace source="defaultTrace">
        @{
            var returnResponse = new JObject
                (
                    new JProperty("Authenticated", false),
                    new JProperty("Source", context.LastError.Source),
                    new JProperty("Reason", context.LastError.Reason),
                    new JProperty("Message", context.LastError.Message),
                    new JProperty("Scope", context.LastError.Scope),
                    new JProperty("Section", context.LastError.Section),
                    new JProperty("Path", context.LastError.Path),
                    new JProperty("PolicyId", context.LastError.PolicyId)
                ).ToString();
            return returnResponse.ToString();
        } 
    </trace>
    <return-response>
        <set-status code="500" reason="Error" />
        <set-body>
            @{
                var debuggingVariable = context.Variables.GetValueOrDefault<string>("debugging");
                if(debuggingVariable == "1")
                {
                    var returnResponse = new JObject
                        (
                            new JProperty("Authenticated", false),
                            new JProperty("Source", context.LastError.Source),
                            new JProperty("Reason", context.LastError.Reason),
                            new JProperty("Message", context.LastError.Message),
                            new JProperty("Scope", context.LastError.Scope),
                            new JProperty("Section", context.LastError.Section),
                            new JProperty("Path", context.LastError.Path),
                            new JProperty("PolicyId", context.LastError.PolicyId)
                        ).ToString();
                    return returnResponse.ToString();
                }
                else
                {
                    var returnResponse = new JObject(new JProperty("Authenticated", false)).ToString();
                    return returnResponse.ToString();
                }
            }
        </set-body>
    </return-response>
</on-error>

该策略执行以下操作:

入境

  • 在整个过程中编写一些跟踪语句
  • 从查询字符串中获取几个参数以在策略中使用
  • 删除几个参数,因为我们无法将这些参数传递给后端服务,因为调用与方法签名不匹配

BACK-END - 发送请求

  • 使用已通过但显然没有我们删除的参数的现有 URL 向后端服务发送请求
  • 将方法设置为 POST
  • 使用另一个参数设置消息正文

出境

  • 如果我们收到 200 响应,那么我们知道后端服务正常运行
  • 设置一个名为microservice-response的变量来存储来自后端服务的响应
  • 设置将在生产中删除的调试变量。这样测试人员才能得到更详细的信息。我们将向他们显示微服务响应的内容
  • 如果后端服务出现问题(不是 200 响应),则抛出错误

错误

  • 当 APIM 捕获错误时。从 context.LastError 变量中提取所有详细信息,如果处于调试模式,则发送给调用者

这是 context.LastError 的示例: 在此处输入图像描述

我希望这对某人有所帮助,因为我花了很长时间才达到这一点。这方面的文档很好,但那里没有多少

于 2016-11-22T21:50:59.757 回答
1

您可以使用标签更改请求的 HTTP 方法。请参阅https://msdn.microsoft.com/en-us/library/azure/dn894085.aspx#SetRequestMethod了解更多信息

于 2016-09-15T14:52:27.493 回答
0

inbound要更改的操作的策略部分,您可以使用 set-method 策略更改请求的 HTTP 方法。

<inbound>
      <set-method>POST</set-method>
</inbound>
于 2016-09-21T15:28:01.410 回答