如何在响应中将两个变量合并为一个?

编程入门 行业动态 更新时间:2024-10-28 06:22:25
本文介绍了如何在响应中将两个变量合并为一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

    更多推荐

    如何在响应中将两个变量合并为一个?

    本文发布于:2023-10-19 14:25:02,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1507790.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:并为   中将   变量   两个   如何在

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!