如何从xml soap发出http请求?

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

我正在我的应用程序中实现一个支付网关(预付现金)。这是链接:

Adv documentaion

根据其文件,我必须提出交易请求。但请求以xml格式提及。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:wsm="http://wsm.advcash/">
  <soapenv:Header/>
  <soapenv:Body>
    <wsm:sendMoney>
      <arg0>
        <apiName>api_name</apiName>
        <authenticationToken>token</authenticationToken>
        <accountEmail>[email protected]</accountEmail>
      </arg0>
      <arg1>
        <amount>1.00</amount>
        <currency>USD</currency>
        <email>[email protected]</email>
        <note>Some note</note>
        <savePaymentTemplate>false</savePaymentTemplate>
      </arg1>
    </wsm:sendMoney>
  </soapenv:Body>
</soapenv:Envelope>

我认为这是一种肥皂请求,我想知道如何请求这个以及我必须使用我的节点js应用程序使用“请求”npm模块发送的参数。请帮助。

node.js xml soap soap-client
2个回答
0
投票

你应该使用node-soap包。有一个请求选项覆盖请求模块。


0
投票

我做了一个样本,看一看

service.ts

const service = {
  UserService: {
    ServicePort: {
      getUserById(args) {
        const user = { id: args.id, name: faker.name.findName(), email: faker.internet.email() };
        return user;
      }
    }
  }
};

service.wsdl

<definitions name="Service" targetNamespace="http://localhost:8001/get-started/service.wsdl" 
  xmlns="http://schemas.xmlsoap.org/wsdl/" 
  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
  xmlns:tns="http://localhost:8001/get-started/service.wsdl" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <message name="GetUserByIdInput">
    <part name="id" type="xsd:string"></part>
  </message>

  <message name="GetUserByIdOutput">
    <part name="id" type="xsd:string"></part>
    <part name="name" type="xsd:string"></part>
    <part name="email" type="xsd:string"></part>
  </message>

  <portType name="ServicePortType">
    <operation name="getUserById">
      <input message="tns:GetUserByIdInput"/>
      <output message="tns:GetUserByIdOutput"></output>
    </operation>
  </portType>

  <binding name="ServiceSoapBinding" type="tns:ServicePortType">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="getUserById">
      <soap:operation soapAction="getUserById"/>
      <input message="tns:GetUserByIdInput">
        <soap:body parts="id" use="literal"/>
      </input>
      <output message="tns:GetUserByIdOutput">
        <soap:body parts="id" use="literal"/>
        <soap:body parts="name" use="literal"/>
        <soap:body parts="email" use="literal"/>
      </output>
    </operation>
  </binding>

  <service name="UserService">
    <documentation>Get started service</documentation>
    <port name="ServicePort" binding="tns:ServiceSoapBinding">
      <soap:address location="http://localhost:8001/get-started"/>
    </port>
  </service>

</definitions>

request.xml

<?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="http://localhost:8001/get-started/service.wsdl">
  <soap:Body>
    <tns:getUserById>
      <id>8104d3c3-de13-432f-b4a0-a62f84f6206a</id>
    </tns:getUserById>
  </soap:Body>
</soap:Envelope>

server.spec.ts

it('should get correct result using http request and static xml', async (done: jest.DoneCallback) => {
    const xml = fs.readFileSync(path.resolve(__dirname, './static-xml.xml'), 'utf8');
    const uuid = '8104d3c3-de13-432f-b4a0-a62f84f6206a';
    const options = {
      url,
      method: 'POST',
      body: xml,
      headers: {
        'Content-Type': 'text/xml;charset=utf-8',
        'Accept-Encoding': 'gzip,deflate',
        'Content-Length': xml.length
      }
    };

    const rawXml = await request(options);
    parser.parseString(rawXml, (err, actualValue) => {
      if (err) {
        done(err);
      }
      console.log('actualValue: ', JSON.stringify(actualValue));
      expect(actualValue['soap:Envelope']['soap:Body']['tns:getUserByIdResponse']['tns:id']).toBe(uuid);
      done();
    });
  });

结果:

actualValue:  {"soap:Envelope":{"$":{"xmlns:soap":"http://schemas.xmlsoap.org/soap/envelope/","xmlns:tns":"http://localhost:8001/get-started/service.wsdl"},"soap:Body":{"tns:getUserByIdResponse":{"tns:id":"bf0f6172-2f53-4b33-94c8-9ff9ed8fd431","tns:name":"Theo Leannon","tns:email":"[email protected]"}}}}

这是演示:https://github.com/mrdulin/nodejs-soap/tree/master/src/get-started

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