1

I'm trying to build an API in azure and trying to modify the incoming json request using liquid template via set body policy. But the json elements are coming as null

Incoming json request

{
  "GetOrderDetailsResponse": {
    "OrderId": 1,
    "OrderDate": "2018-08-13",
    "OrderLines": {
      "OrderLine": [
        {
          "Product": "Pizza Margherita",
          "Amount": 5
        },
        {
          "Product": "Pizza Calzone",
          "Amount": 2
        },
        {
          "Product": "Pizza Funghi",
          "Amount": 1
        }
      ]
    }
  }
}

Policy code

<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>

Response is blank

{
    "orderId": "",
    "orderDate": "",
    "orderLines": []
}

I'm new to this,please advise if i'm missing something obvious

Unicorn
  • 11
  • 1

1 Answers1

1

As mentioned here in the official docs, the Content-Type header must be set to application/json for the body to parsed and available to the liquid template.

In this case, you could either make sure the request sent has it set or manually set it in the inbound scope (not inside return-response) using the set-header policy

PramodValavala
  • 6,026
  • 1
  • 11
  • 30