json - Azure APIM-无法访问液体模板中的 json 正文值

标签 json azure templates liquid apim

我正在尝试在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/

相关文章:

java - 用于 Java 的类 PHP 表示语言

templates - elasticsearch-索引模板创建错误-解析异常,未知 key

arrays - 用不同的数组值替换JSON中的符号

javascript - Highcharts 折线图 - 数据点上的超链接

mysql - 如何使用 node.js MySQL 从 MySql DB 获取结果并将它们发送回 API.ai - DialogFlow

security - 使用 OWIN/Katana 在 Azure 上进行数据保护操作失败

c++ - 继承一个带有内部类的模板类,并在继承类中访问内部类

java - Jackson JSON 使用注释进行 pretty-print

azure - Azure 上的 DocumentDb - 我无法在一次运行中获取完整的记录集?

json - 微软学术API,知识图谱搜索——检索到的论文为空