如何在响应中将两个变量合并为一个? [英] How to merge two variables into one in a response?

查看:136
本文介绍了如何在响应中将两个变量合并为一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我向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>https://env1-api.azure-api.net/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>https://env2-api.azure-api.net/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:

获取对"> https://env1-api.azure-api.net的请求/api/data "已发送,结果存储在"env1"变量中.

GET request to 'https://env1-api.azure-api.net/api/data' has been sent, result stored in 'env1' variable.

获取对"> https://env2-api.azure-api.net的请求/api/data "已发送,结果存储在"env2"变量中.

GET request to 'https://env2-api.azure-api.net/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的答案中,请在

To add to Aleksander's answer, inside the return-response policy, there are two ways you can compose the final body

  1. 使用策略表达式

<set-body>@{
    var output = new
    {
        success = true,
        var1 = context.Variables["var1"]
    };
    return JsonConvert.SerializeObject(output);
}</set-body>

  1. 使用液体模板

<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.

这篇关于如何在响应中将两个变量合并为一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆