Azure APIM 从 JSON 到 XML 的液体转换不适用于 for 循环

问题描述 投票:0回答:1

我目前遇到了与以下线程中描述的问题类似的问题,确实需要一些帮助 -

循环的 Azure API 管理策略不适用于阵列

我已经实现了确切的解决方案,但模板仍然无法读取值数组。

这是我的转换代码。

<json-to-xml apply="always" consider-accept-header="true" parse-date="true" />
<set-body template="liquid">
    <soapenv:Envelope >
        <soapenv:Body>
            <Create xmlns="http://siebel.com/WebService">
                <SharedList>
                    {% for item in body.sharedList.contact %}
                        <contact>
                            {% if item.userId %}
                            <UserId>{{item.userId}}</UserId>
                            {% else %}
                            <UserId>Empty</UserId>
                            {% endif %}
                        </contact>
                    {% endfor %}
                </SharedList>
                <AssetNum>{{body.Document.AssetNum}}</AssetNum>
            </CreateSR_Input>
        </soapenv:Body>
    </soapenv:Envelope>
</set-body>
<!-- Set Content-Type header to XML -->
<set-header name="Content-Type" exists-action="override">
    <value>application/xml</value>
</set-header>

我很感激任何建议。

这是示例有效负载。

{
 "assetNum": "",
 "sharedList": {
    "contact": [
            {
            "userId": "spokuri"
            }
        ]
    }
}
azure-api-management apim
1个回答
0
投票

您需要修改策略并将

Content-Type
添加到
application/json
,然后按照@Markus MeyerSO 中的建议开始液体转换。

<policies>
    <inbound>
        <base />
        <set-header name="Content-Type" exists-action="override">
            <value>application/json</value>
        </set-header>
        <set-body template="liquid">
            <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                <soapenv:Body>
                    <Create xmlns="http://siebel.com/WebService">
                        <SharedList>
                            {% for item in body.sharedList.contact %}
                            <contact>
                                {% if item.userId %}
                                <UserId>{{item.userId}}</UserId>
                                    {% else %}
                                <UserId>Empty</UserId>
                                {% endif %}
                            </contact>
                            {% endfor %}
                        </SharedList>
                        <AssetNum>{{body.Document.AssetNum}}</AssetNum>
                    </Create>
                </soapenv:Body>
            </soapenv:Envelope>
        </set-body>
        <set-header name="Content-Type" exists-action="override">
            <value>application/xml</value>
        </set-header>
    </inbound>
</policies>

这会给你预期的回应。

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.