基于
Azure API Management Service "Set body" Policy
我们可以修改API服务的响应。例如,而不是返回下面:
{
"company" : "Azure",
"service" : "API Management"
}
我们只想回来:
{ "company" : "Azure" }
我不知道如何实现这一目标,因为我不知道他们在文档中使用了哪种编程语言/语法,如下所示:
<set-body>
@{
string inBody = context.Request.Body.As<string>(preserveContent: true);
if (inBody[0] =='c') {
inBody[0] = 'm';
}
return inBody;
}
</set-body>
答案 0 :(得分:2)
您所看到的内容称为Policy expressions
,并在official documentation here上有详细说明。该网站的简短引用声明:
策略表达式语法是C#6.0。每个表达式都可以访问 隐式提供了上下文变量和.NET的允许子集 框架类型。
set-body
samples中的更合适的样本将是过滤输出的样本:
<!-- Copy this snippet into the outbound section to remove a number of data elements from the response received from the backend service based on the name of the api product -->
<set-body>@{
var response = context.Response.Body.As<JObject>();
foreach (var key in new [] {"minutely", "hourly", "daily", "flags"}) {
response.Property (key).Remove ();
}
return response.ToString();
}
</set-body>
要为特定对象自定义 - 您要删除service
属性:
<set-body>@{
var response = context.Response.Body.As<JObject>();
foreach (var key in new [] {"service"}) {
response.Property (key).Remove ();
}
return response.ToString();
}
</set-body>