在策略定义中返回响应时如何使用变量? [英] How to use a variable when returning response in policy definition?
问题描述
我正在Azure API管理实例中配置入站策略.
I'm configuring inbound policies in an instance of Azure API Management.
首先,我设置一个变量:
First, I set a variable:
<set-variable name="var1" value="" />
然后我发送一个请求
<send-request mode="new" response-variable-name="var1" timeout="20" ignore-error="false">
哪个返回JSON.测试时,我在跟踪"选项卡中收到以下消息:
Which returns a JSON. When testing I get the following message in trace tab:
获取对"> https://my-api.azure-api.net的请求/api/data "已发送,结果存储在"var1"变量中.
GET request to 'https://my-api.azure-api.net/api/data' has been sent, result stored in 'var1' variable.
我猜发送请求策略有效,结果存储在变量中. 然后我想返回一个响应(仍然在入站,尝试在出站时得到500):
I guess the send-request policy works and the result is stored in the variable. Then I want to return a response (still in inbound, I get 500 when trying to do it in outbound):
<return-response response-variable-name="existing response variable">
<set-status code="200" reason="OK" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>
{
"success": true,
"var1": context.Variables["var1"]
}
</set-body>
</return-response>
我的问题是它不起作用...它只是呈现context.Variables["var1"]
.
My problem is it doesn't work... It just renders context.Variables["var1"]
.
也是如此:
-
@context.Variables["var1"]
-
@{ context.Variables.GetValueOrDefault<string>("var1") }
-
@context.Variables.GetValueOrDefault("var1")
@context.Variables["var1"]
@{ context.Variables.GetValueOrDefault<string>("var1") }
@context.Variables.GetValueOrDefault("var1")
所有这些均以书面形式呈现,未提取任何值.
All of them are rendered as written, no value is being extracted.
我还尝试添加一个占位符字符串,然后使用
I also tried adding a placeholder string and then using
<find-and-replace from="Placeholder" to="context.Variables.GetValueOrDefault("var1")" />
并尝试将其放入入站和出站.但是这项政策没有启动.
And try to place it in inbound and outbound alike. But this policy did not launch.
这是我想附加到响应中的JSON对象(小细节:实际上,我有多个变量的问题).
It's a JSON object that I want to append to the response (small detail: in reality I have this issue with multiple variables).
我的问题是:如何将声明的变量添加到响应中?
My question is: how can I add my declared variable to the response?
推荐答案
有两种方法可以解决此问题.您可以为此使用策略表达式: https ://docs.microsoft.com/zh-CN/azure/api-management/api-management-policy-expressions .要记住的是,它们只能用于构建政策的整体价值,而不能构成其一部分,因此:
There are two ways you can go about this. You could to use policy expressions for that: https://docs.microsoft.com/en-us/azure/api-management/api-management-policy-expressions. The thing to remember is that they can only be used to construct whole value for policy, not part of it, so:
<set-body>@("{\"success\": true, \"var1\": " + ((IResponse)context.Variables["var1"]).Body.As<string>() + "}"</set-body>
或者通过set-body
策略,您可以使用液体模板:
Or with set-body
policy you could use liquid template:
<set-variable name="var1body" value="@((IResponse)context.Variables["var1"]).Body.As<string>())" />
<set-body template="liquid">
{
"success": true,
"var1": {{context.Variables["var1body"]}}
}
</set-body>
这篇关于在策略定义中返回响应时如何使用变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!