基于
Azure API管理服务"设置车身"策略
我们可以修改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>
您所看到的称为 Policy expressions
,在此处的官方文档中得到很好的描述。网站的简短报价:
策略表达语法是c#6.0。每个表达式都可以访问 隐式提供了上下文变量和.NET的允许子集 框架类型。
set-body
中更合适的样本将是过滤输出的样本:
<!-- 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>