Azure API 管理服务集正文策略 - 修改 JSON 对 return 某些字段的响应
Azure API Management Service Set body Policy - modify JSON response to return certain fields
基于
Azure API Management Service "Set body" Policy
我们可以修改 API 服务的响应。例如,而不是 returning 下面:
{
"company" : "Azure",
"service" : "API Management"
}
我们只想 return :
{ "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
,在 official documentation here 上有很好的描述。该网站的简短引述如下:
Policy expressions syntax is C# 6.0. Each expression has access to the
implicitly provided context variable and an allowed subset of .NET
Framework types.
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>
基于
Azure API Management Service "Set body" Policy
我们可以修改 API 服务的响应。例如,而不是 returning 下面:
{
"company" : "Azure",
"service" : "API Management"
}
我们只想 return :
{ "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
,在 official documentation here 上有很好的描述。该网站的简短引述如下:
Policy expressions syntax is C# 6.0. Each expression has access to the implicitly provided context variable and an allowed subset of .NET Framework types.
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>