Help us understand the problem. What is going on with this article?

nodejsでSOAP通信する

More than 1 year has passed since last update.

NodejsでSOAP通信をする必要があり、以下のモジュールを採用して実装しました。

soapモジュール

https://github.com/vpulim/node-soap

使い方はシンプル。

WSDLが以下の内容でExecutePaymentを実行したかったとすると、

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" targetNamespace="http://www.soap.com/" xmlns:tns="http://www.soap.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace="http://www.soap.com/">
      <s:element name="ExecutePayment">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" name="InParam" type="tns:RequestParam" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:complexType name="RequestParam">
        <s:sequence>
          <s:element minOccurs="0" name="UserNo" type="s:string" />
          <s:element minOccurs="0" name="DealNo" type="s:string" />
          <s:element minOccurs="0" name="Amount" type="s:string" />
        </s:sequence>
      </s:complexType>
      <s:element name="ExecutePaymentResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" name="OutParam" type="tns:ResponseParam" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:complexType name="ResponseParam">
        <s:sequence>
          <s:element minOccurs="0" name="Result" type="s:string" />
        </s:sequence>
      </s:complexType>
    </s:schema>
  </wsdl:types>
  <wsdl:message name="ExecutePaymentIn">
    <wsdl:part name="parameters" element="tns:ExecutePayment" />
  </wsdl:message>
  <wsdl:message name="ExecutePaymentOut">
    <wsdl:part name="parameters" element="tns:ExecutePaymentResponse" />
  </wsdl:message>
  <wsdl:portType name="PaymentSoapPort">
    <wsdl:operation name="ExecutePayment">
      <wsdl:input message="tns:ExecutePaymentIn" />
      <wsdl:output message="tns:ExecutePaymentOut" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="PaymentSoapBinding" type="tns:PaymentSoapPort">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
    <wsdl:operation name="ExecutePayment">
      <soap:operation soapAction="http://www.soap.com/ExecutePayment" style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="Payment">
    <wsdl:port name="PaymentSoapPort" binding="tns:PaymentSoapBinding">
      <soap:address location="http://www.soap.com/" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

jsでは以下のように書くことで実行できます。

var soap = require('soap');

// パラメータを作成
var args = {
  InParam: {
    UserNo: '77770000001',
    DealNo: '20170701000001',
    Amount: '2160'
  }
};

// 実行
soap.createClient(
  'http://www.soap.com/?wsdl' // エンドポイント
  , {} // オプションとして付加したい情報があればセット
  , function(err, client) {
    // Function実行
    client.ExecutePayment(
      // パラメータ
      args
      , function(err, result) {
        // 結果の処理
        var resObj = result.OutParam;
        console.log(resObj.Result);
      }
    );
  }
);

パラメータや結果データはJSON形式で指定・使用します。

BaicAuthのほか、Bearerやクライアント証明書のセットなんかも対応している。
https://github.com/vpulim/node-soap#security

Headerを独自に指定したければ、addSoapHeaderを使用して実装することが出来ます。
https://github.com/vpulim/node-soap#outgoing-soap-headers

    client.addSoapHeader({
      HeaderName: {
        hoge: 'xxxx'
      }
    });
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away