API 管理策略 - 通过 x-www-form-urlencoded 正文

API Management Policy - pass x-www-form-urlencoded body

我有一个旧的 asmx 网络服务,可以像这样使用邮递员调用

我想通过 API 管理将其公开为 JSON 端点,然后有一个转换策略 XML 但我不确定如何在政策

我已尝试在下面执行此操作(及其变体),但我总是收到消息错误 'requestXML is missing'

<set-body template="liquid">
            <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <soap:Body>
                    requestXml: "<Request Caller="Harness"><GetEmployerOfferings><EmployerCode>CHCA</EmployerCode></GetEmployerOfferings></Request>"
                </soap:Body>
            </soap:Envelope>
        </set-body>
        <set-header name="Content-Type" exists-action="override">
            <value>application/x-www-form-urlencoded</value>
        </set-header>

如何通过 APIM 策略将其传递给后端服务?

根据您的要求,我对您在评论中提出的 json 示例感到有些困惑。不过我也可以提供一些信息给大家参考。

1. 如果你想测试将 requestXml 传递给后端的硬代码,正确的格式应该是 requestXml=xxxx 而不是 requestXml:xxxx 因为你使用“x-www-form-urlencoded”作为content-type(在邮递员中我们可以使用requestXml:xxxx)。所以apim中的policy应该是:

<set-body>requestXml=<Request Caller="Harness"><GetEmployerOfferings><EmployerCode>CHCA</EmployerCode></GetEmployerOfferings></Request></set-body>

但由于您的正文包含 xml <>,因此它会在保存策略后自动删除 <Request Caller="Harness"><GetEmployerOfferings><EmployerCode>CHCA</EmployerCode></GetEmployerOfferings></Request>。只留下<set-body>requestXml=</set-body>,硬编码测试可能不会成功。

2. 据我了解,如果您想使用如下 json 数据请求 APIM:

{
    "getEmployerOfferings": {
        "requestXml": "<Request Caller=\"Harness\"><GetEmployerOfferings><EmployerCode>CHCA</EmployerCode></GetEmployerOfferings></Request>"
    }
}

如果您的要求json如上例,您可以参考以下政策:

<inbound>
    <base />
    <set-body>@{
        var request = context.Request.Body.As<JObject>();
        var xmlstring = request["getEmployerOfferings"]["requestXml"].ToString();
        var result = "requestXml=" + xmlstring;
        return result;
    }</set-body>
    <set-header name="Content-Type" exists-action="override">
        <value>application/x-www-form-urlencoded</value>
    </set-header>
</inbound>

测试apim,运行后可以找到最终的request body,显示: