首页 > 解决方案 > Node.js SOAP(包):如何将 XML 示例转换为查询?

问题描述

不幸的是,我们的合作伙伴只有 XML-SOAP 中的 API。这是我第一次体验它。

我有 XML 中成功请求的示例:

<?xml version="1.0" encoding="UTF-8"?>

<soap:Envelope soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"

    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"

    xmlns:xsd="http://www.w3.org/2001/XMLSchema"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <soap:Body>

        <GetMessageRequest xmlns="urn://x-artefacts-gnivc-ru/inplat/servin/OpenApiMessageConsumerService/types/1.0">

            <Message>

                <tns:AuthRequest xmlns:tns="urn://x-artefacts-gnivc-ru/ais3/kkt/AuthService/types/1.0">

                    <tns:AuthAppInfo>

                        <tns:MasterToken>[ I NEED TO SEND ONLY THIS VARIABLE ]</tns:MasterToken>

                    </tns:AuthAppInfo>

                </tns:AuthRequest>

            </Message>

        </GetMessageRequest>

    </soap:Body>

</soap:Envelope>

我需要了解什么是什么:这里的功能是什么(如果我理解正确 - 它是 GetMessageRequest,请纠正我),什么是参数(如果我理解 - 它们在消息内部,但如何正确键入它们)。

如何编写正确的查询?

我的代码:

        require('soap')
            .createClientAsync('https://openapi.nalog.ru:8090/open-api/AuthService/0.1?wsdl')
            .then(client => client.GetMessageAsync({Message: {
                AuthRequest: {
// in XML there is tns:AuthRequest
// xmlns:tns="urn://x-artefacts-gnivc-ru/ais3/kkt/AuthService/types/1.0" 
// How to add it here correct?

                    AuthAppInfo: {

                    },
                }}})
                .then(message => log(message)).catch(err => inspect(err)));

标签: node.jssoapnode-soap

解决方案


SOAP 的主要部分是 WSDL。有了它,您可以生成客户端来调用公开的服务。

对于主要的 wsdl 标签,您可以参考w3cshool

从此开始

<wsdl:operation name="GetMessage">
<wsdl:input message="tns:GetMessageRequest" name="GetMessageRequest"> </wsdl:input>
<wsdl:output message="tns:GetMessageResponse" name="GetMessageResponse"> </wsdl:output>
<wsdl:fault message="tns:AuthenticationException" name="AuthenticationException"> </wsdl:fault>
</wsdl:operation>

这是一个你可以调用它的函数,你可以看到它接受什么请求,响应格式是什么,故障格式是什么。一个简单的 xsdtype描述了每条消息的格式。

对于您的用例,此代码段是一个好的开始:

const soap = require('soap');
const url = 'https://openapi.nalog.ru:8090/open-api/AuthService/0.1?wsdl';
const args = {
  GetMessageRequest:
    { Message: { AuthRequest: { AuthAppInfo: { MasterToken: 'TOKEN' } } } }
};
soap.createClient(url, function (err, client) {
  console.log(JSON.stringify(client.describe(), null, 2));
  client.GetMessage(args, function (err, result) {
    console.log(result);
  });
});

它产生这个请求:

<?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="urn://x-artefacts-gnivc-ru/inplat/servin/OpenApiMessageConsumerService/1.0"
    xmlns:ns1="urn://x-artefacts-gnivc-ru/inplat/servin/OpenApiMessageConsumerService/types/1.0">
    <soap:Body>
        <ns1:GetMessageRequest
            xmlns:ns1="urn://x-artefacts-gnivc-ru/inplat/servin/OpenApiMessageConsumerService/types/1.0"
            xmlns="urn://x-artefacts-gnivc-ru/inplat/servin/OpenApiMessageConsumerService/types/1.0">
            <ns1:GetMessageRequest>
                <ns1:Message>
                    <ns1:AuthRequest>
                        <ns1:AuthAppInfo>
                            <ns1:MasterToken>TOKEN</ns1:MasterToken>
                        </ns1:AuthAppInfo>
                    </ns1:AuthRequest>
                </ns1:Message>
            </ns1:GetMessageRequest>
        </ns1:GetMessageRequest>
    </soap:Body>
</soap:Envelope>

请注意,AuthRequest需要将命名空间固定为urn://x-artefacts-gnivc-ru/ais3/kkt/AuthService/types/1.0


推荐阅读