我向Azure API管理添加了两个API.它基本上是相同的API,但适用于不同的环境.为了进行监视,我想创建一个在两个API中都调用相同方法的操作,然后将其结果合并为一个.我目前正在使用具有模拟数据的模拟API.
I have two APIs added to Azure API Management. It's basically the same API, but for different environments. For monitoring purposes, I want to create an operation that would call the same method in both of the APIs and merge their result into one. I currently work on mocked APIs with mocked data.
为此,我创建了一个带有空白操作的空白API.在此操作中,我声明了以下入站策略:
To achieve that I created a blank API with a blank operation. Inside this operation, I declared the following inbound policies:
<inbound> <set-variable name="env1" value="" /> <set-variable name="env2" value="" /> <send-request mode="new" response-variable-name="env1" timeout="20" ignore-error="false"> <set-url>env1-api.azure-api/api/data</set-url> <set-method>GET</set-method> <set-header name="Content-Type" exists-action="override"> <value>application/json</value> </set-header> </send-request> <send-request mode="new" response-variable-name="env2" timeout="20" ignore-error="false"> <set-url>env2-api.azure-api/api/data</set-url> <set-method>GET</set-method> <set-header name="Content-Type" exists-action="override"> <value>application/json</value> </set-header> </send-request> <base /> </inbound>经过测试,称为throws 500的操作(我认为这是可以理解的,因为未设置任何响应).当我查看跟踪"选项卡时,我会看到两条消息:
When tested the operation called throws 500 (which, I believe, is understandable, as no response is being set). When I look at the Trace tab I can see two messages:
获取对"> env1-api.azure-api的请求/api/data "已发送,结果存储在"env1"变量中.
GET request to 'env1-api.azure-api/api/data' has been sent, result stored in 'env1' variable.
获取对"> env2-api.azure-api的请求/api/data "已发送,结果存储在"env2"变量中.
GET request to 'env2-api.azure-api/api/data' has been sent, result stored in 'env2' variable.
基于此,我得出的结论是这些呼叫正常运行.这就是我卡住的地方.我不知道如何在响应中合并这两个变量.
Based on that I conclude that the calls are working correctly. Here's where I'm stuck. I don't know how to merge those two variables inside a response.
API以JSON对象的形式返回对象数组.我要实现的是将这两个响应合并为一个将由操作返回的响应.如何撰写回应?
The API's return an array of objects in the form of a JSON object. What I want to achieve is to merge those two responses into one response that will be returned by the operation. How can I compose a response?
请记住,我是Azure的菜鸟,所以我的方法可能太原始了.如果您有更好的东西,我很想听听.
Please have in mind that I'm a noob in Azure, so my approach might be too primitive. If you have something better I'd love to hear about it.
推荐答案要添加到Aleksander的答案中,请在 return-response策略,有两种方法可以组成最终主体
To add to Aleksander's answer, inside the return-response policy, there are two ways you can compose the final body
<set-body>@{ var output = new { success = true, var1 = context.Variables["var1"] }; return JsonConvert.SerializeObject(output); }</set-body>
<set-variable name="var1body" value="@((IResponse)context.Variables["var1"]).Body.As<string>())" /> <set-body template="liquid">{ "success": true, "var1": "{{context.Variables["var1body"]}}" }</set-body>
您可以阅读有关 set-body策略在其文档中.
You can read more about the set-body policy in its doc.
更多推荐
如何在响应中将两个变量合并为一个?
发布评论