我正在尝试在azure中构建一个API,并尝试通过设置正文策略使用液体模板修改传入的json请求。但 json 元素为 null
传入 json 请求
{
"GetOrderDetailsResponse": {
"OrderId": 1,
"OrderDate": "2018-08-13",
"OrderLines": {
"OrderLine": [
{
"Product": "Pizza Margherita",
"Amount": 5
},
{
"Product": "Pizza Calzone",
"Amount": 2
},
{
"Product": "Pizza Funghi",
"Amount": 1
}
]
}
}
}
政策代码
<policies>
<inbound>
<base />
<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 template="liquid">
{
"orderId" : "{{body.GetOrderDetailsResponse.OrderId}}",
"orderDate" : "{{body.GetOrderDetailsResponse.OrderDate | Date: "dd/MM/yyyy"}}",
"orderLines" : [
{% JSONArrayFor orderline in body.GetOrderDetailsResponse.OrderLines %}
{
"product" : "{{orderline.Product}}",
"amount" : "{{orderline.Amount}}"
}
{% endJSONArrayFor %}
]
}
</set-body>
</return-response>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<!--Create JSON Response via liquid template-->
</outbound>
<on-error>
<base />
</on-error>
</policies>
回复为空
{
"orderId": "",
"orderDate": "",
"orderLines": []
}
我是新手,如果我遗漏了一些明显的东西,请告知
最佳答案
正如 official docs 中所述,Content-Type
header 必须设置为 application/json
才能解析正文并可用于 Liquid 模板。
在这种情况下,您可以确保发送的请求已设置,或者使用 set-header
policy 在入站范围内(不在 return-response
内)手动设置它。
关于json - Azure APIM-无法访问液体模板中的 json 正文值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73262394/