Azure API Management policy for loop not working on array

  Kiến thức lập trình

In Azure API Management I have a SOAP to REST api with the wsdl imported. I am able to send requests through however the liquid template in the set-body which creates the XML request from JSON is not able to read an array in the JSON.

Liquid Template:

<set-body template="liquid">
            <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://tempuri.org/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <soap:Body>
                    <testEndpointName>
                        <request>
                            <Attachments>
                                {% for item in body.testEndpointName.request.attachments %}
                                <testEndpointNameAttachment>
                                    {% if item.fileBytes %}
                                    <FileBytes>{{item.fileBytes}}</FileBytes>
                                    {% else %}
                                    <FileBytes xsi:nil="true" />
                                    {% endif %}
                                    {% if item.fileName %}
                                    <FileName>{{item.fileName}}</FileName>
                                    {% else %}
                                    <FileName xsi:nil="true" />
                                    {% endif %}
                                    {% if item.fileSize %}
                                    <FileSize >{{item.fileSize}}</FileSize>
                                    {% else %}
                                    <FileSize xsi:nil="true" />
                                    {% endif %}
                                </testEndpointNameAttachment>
                                {% endfor %}
                            </Attachments>
                            {% if body.testEndpointName.request.email %}
                            <Email>{{body.testEndpointName.request.email}}</Email>
                            {% else %}
                            <Email xsi:nil="true" />
                            {% endif %}
                            {% if body.testEndpointName.request.emailBody %}
                            <EmailBody">{{body.testEndpointName.request.emailBody}}</EmailBody>
                            {% else %}
                            <EmailBody xsi:nil="true" />
                            {% endif %}
                            {% if body.testEndpointName.request.policyNumber %}
                            <PolicyNumber>{{body.testEndpointName.request.policyNumber}}</PolicyNumber>
                            {% else %}
                            <PolicyNumberxsi:nil="true" />
                            {% endif %}
                            {% if body.testEndpointName.request.subject %}
                            <Subject>{{body.testEndpointName.request.subject}}</Subject>
                            {% else %}
                            <Subject xsi:nil="true" />
                            {% endif %}
                        </request>
                    </testEndpointName>
                </soap:Body>
            </soap:Envelope>
        </set-body>

Example Input:

{
    "testEndpointName": {
        "request": {
            "attachments": [
                {
                    "fileBytes": "fakebytes",
                    "fileName": "fakename.txt",
                    "fileSize": "1234"
                }
            ],
            "email": "[email protected]",
            "emailBody": "test email body",
            "policyNumber": "1234",
            "subject": "test subject"
        }
    }
}

This gets me a request which looks like the following:

<soapenv:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://tempuri.org/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <testEndpointName>
         <request>
            <Attachments></Attachments>
            <Email>[email protected]</Email>
            <EmailBody>test email body</EmailBody>
            <PolicyNumber>1234</PolicyNumber>
            <Subject>test subject</Subject>
         </request>
      </testEndpointName>
   </soapenv:Body>
</soapenv:Envelope>

The Attachments section is always empty. I have tried replacing for with JSONArrayFor and also made sure that the header Content-Type is always set to application/json. I also tried hard coding values inside the for loop to see if it ever got in there and it did not.

Any help would be appreciated.

4

Before doing the liquid-transformation, it has to be ensured that the Content-Type is set to application/json, like the input data.
This can also be done in the request itself, but to be on the safe side, it can be set in the policy.

The response Content-Type application/xml has to be set after the transformation.

Policy:

<set-header name="Content-Type" exists-action="override">
    <value>application/json</value>
</set-header>
<set-body template="liquid">
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://tempuri.org/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <soap:Body>
            <testEndpointName>
                <request>
                    <Attachments>
                                {% for item in body.testEndpointName.request.attachments %}
                                <testEndpointNameAttachment>
                                    {% if item.fileBytes %}
                                    <FileBytes>{{item.fileBytes}}</FileBytes>
                                    {% else %}
                                    <FileBytes xsi:nil="true" />
                                    {% endif %}
                                    {% if item.fileName %}
                                    <FileName>{{item.fileName}}</FileName>
                                    {% else %}
                                    <FileName xsi:nil="true" />
                                    {% endif %}
                                    {% if item.fileSize %}
                                    <FileSize>{{item.fileSize}}</FileSize>
                                    {% else %}
                                    <FileSize xsi:nil="true" />
                                    {% endif %}
                                </testEndpointNameAttachment>
                                {% endfor %}
                    </Attachments>
                </request>
            </testEndpointName>
        </soap:Body>
    </soap:Envelope>
</set-body>
<set-header name="Content-Type" exists-action="override">
    <value>application/xml</value>
</set-header>

Test in APIM:

1

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT