【发布时间】:2020-11-16 10:07:30
【问题描述】:
我对 Azure APIM 比较陌生,我有这个后端,我可以调用它以格式返回响应
{
"data": {
"attributes": {
"municipalities": []
}
}
}
我想修改响应,以便以格式返回数据
{
"data": {
"municipalities": []
}
}
我尝试过使用设置的体液模板
<outbound>
<set-body template="liquid">
{
"data": {
"municipalities": {{body.data.attributes.municipalities}}
}
}</set-body>
</outbound>
但我得到的回应只是
{
"data": {
"municipalities":
}
}
如果有人可以向我指出我做错了什么或者是否有更好的方法来做到这一点?
我还尝试使用以下代码来检查我是否能够检索“数据”属性,但在 Azure APIM 测试的跟踪部分中出现以下错误
<outbound>
<set-body>
@{
JObject inBody = context.Request.Body.As<JObject>();
return inBody["data"].ToString();
}
</set-body>
</outbound>
错误:
{
"messages": [
{
"message": "Expression evaluation failed.",
"expression": " \n JObject inBody = context.Request.Body.As<JObject>();\n return inBody[\"data\"].ToString(); \n",
"details": "Object reference not set to an instance of an object."
},
"Expression evaluation failed. Object reference not set to an instance of an object.",
"Object reference not set to an instance of an object."
]
}
【问题讨论】:
-
嗨,乐高,请参阅下面提供的解决方案。如果它对您的问题有帮助,请accept它作为答案(单击我的答案旁边的复选标记,将其从灰色切换为已填充)。先谢谢了~
-
@HuryShen 感谢您的回复,但是如果我的 API 上有 3 条不同的路由,例如 GET /muncipalities、GET /names GET/schools 并且每个路由都有自己的返回模式,这将很棘手例如 body.data.attributes.municipalities 、 body.data.attributes.names 、 body.data.attributes.schools ,每个都有不同的返回值。有没有一种更简单的方法可以从所有中删除 .attributes 部分,而不必分别编写每个数组项的每个属性?因为这将需要为我的 3 条路线制定 3 条单独的政策???
-
Hi LeGo 在谈论“有没有更简单的方法...”之前,我认为您需要检查是否可以在
<outbound><set-body>下获取响应数据。根据一些测试,我猜你在那里没有得到响应数据成功。 -
请参考我回答中的“更新”。
-
抱歉很忙,所以现在只能尝试,但它是完美的:)
标签: azure azure-api-management