首页 > 解决方案 > Node-Soap 参数的 XML 字符串

问题描述

我正在尝试使用此处描述的方法发出肥皂请求:https ://www.npmjs.com/package/soap#example-with-xml-string-for-the-args 。

我想输入一个 xml 字符串而不是 JSON 对象,因为我无法在 JSON 中实现所需的语法。

const url = 'https://www.cashweb.nl/?api/3.0/wsdl';
const xmlStr = "<test></test>"

soap.createClient(url, function (err, client) {
    if (err) {
        response.status(500)
        response.send("error creating client")
    }
    client.Import({ _xml: xmlStr }, function (err, result, rawResponse, soapHeader, rawRequest) {
        // result is a javascript object
        // rawResponse is the raw xml response string
        // soapHeader is the response soap header as a javascript object
        // rawRequest is the raw xml request string
        if (err) {
            response.status(500)
            response.send(err + " " + rawRequest)
        }
        response.status(200)
        response.send(rawRequest + " " + rawResponse)
    })
});

虽然这给了我不需要的输出:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:tns="https://www.cashweb.nl/?api/3.0" xmlns:ns="https://www.cashweb.nl/?api/3.0">
    <soap:Body>
        <tns:Import>
            <_xml>
                <test/>
            </_xml>
        </tns:Import>
    </soap:Body>
</soap:Envelope>

如何摆脱 <_xml> 和标签。我是否错误地使用了该功能?

在 node-soap 测试中找到的示例https://github.com/vpulim/node-soap/blob/70c4f34b6370ffc59e7d4fe88d18d6312ff0bedc/test/client-test.js

  it('should allow passing in XML strings', function (done) {
    soap.createClientAsync(__dirname + '/wsdl/default_namespace.wsdl', _.assign({envelopeKey: 'soapenv'}, meta.options))
      .then(function (client) {
        assert.ok(client);
        var xmlStr = '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n\t<head>\n\t\t<title>404 - Not Found</title>\n\t</head>\n\t<body>\n\t\t<h1>404 - Not Found</h1>\n\t\t<script type="text/javascript" src="http://gp1.wpc.edgecastcdn.net/00222B/beluga/pilot_rtm/beluga_beacon.js"></script>\n\t</body>\n</html>';
        return client.MyOperationAsync({_xml: xmlStr});
      })
      .spread(function (result, raw, soapHeader) {})
      .catch(function (err) {
        done();
      });
  });

我正在尝试完成此操作,因为我的 Soap Api 需要以下方式的 CDATA 标记:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:api="https://www.cashweb.nl/?api">
    <soapenv:Header/>
    <soapenv:Body>
        <api:Import>
            <relatie>202943</relatie>
            <email>helpdesk@cash.nl</email>
            <pass>*********</pass>
            <importData>
                <![CDATA[<?xml version="1.0">
                <CASH>
                    <R0301>
                        <F201>4360</F201>
                        <F302>161201</F302>
                        <F306>kantoorbenodigdheden</F306>
                        <F307>10000</F307>
                        <F901>KAS</F901>
                    </R0301>
                </CASH>]]>
            </importData>
            <administration>
                <api:admCode>demo</api:admCode>
                <api:admMap/>
            </administration>
            <formaat>0</formaat>
        </api:Import>
    </soapenv:Body>
</soapenv:Envelope>

标签: javascriptxmlsoapnode-soap

解决方案


最后,我这样修复它:

function scheduleCash(servicebonnummer, bondatum, servicestatus, tijdstip) {
  const url = "https://www.cashweb.nl/?api/3.0/wsdl";
  const xml = {
    relatie: "",
    email: "",
    pass: "",
    importData: `<![CDATA[<CASH><R3600><F3600>${servicebonnummer}</F3600><F4102>${servicestatus}</F4102><F3601>${bondatum.format(
      "YYMMDD"
    )}</F3601></R3600></CASH>]]>`,
    administration: { admCode: "" },
    formaat: 0
  };

  return soap.createClientAsync(url).then(client => {
    return client.ImportAsync(xml);
  });
}

推荐阅读