使用Node.JS对SOAP进行API访问,脚本不起作用

时间:2019-07-03 01:57:28

标签: node.js api soap

我是SOAP协议的新手,正在生产环境中使用它之前尝试使用工作的测试API。通过SOAP建立成功连接时遇到了一些问题

我研究了数周的帖子,尝试了简单的肥皂请求,肥皂节点,axios和其他模块,但没有成功。

const soapRequest = require('easy-soap-request');
const fs = require('fs');

// example data
const url = 'http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL';
const headers = {
'soapAction': '<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.oorsprong.org/websamples.countryinfo">' +
'<soapenv:Header/>' +
'<soapenv:Body>' +
 '<web:CountryName>' +
    '<web:sCountryISOCode>COL</web:sCountryISOCode>' +
 '</web:CountryName>' +
'</soapenv:Body>' +
'</soapenv:Envelope>'
};
const xml = fs.readFileSync('test/zipCodeEnvelope.xml', 'utf-8');

// usage of module
(async () => {
const { response } = await soapRequest(url, headers, xml, 1000); // 
Optional timeout parameter(milliseconds)
const { body, statusCode } = response;
console.log(body);
console.log(statusCode);
})();

下面列出了错误消息。

C:\Program Files\nodejs>node test.js
SOAP FAIL: Error: Request failed with status code 500
(node:14196) UnhandledPromiseRejectionWarning: 
C:\Program Files\nodejs>node test.js
SOAP FAIL: Error: Request failed with status code 415
(node:14836) UnhandledPromiseRejectionWarning: The server cannot service the request because the media type is unsupported.
(node:14836) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:14836) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

1 个答案:

答案 0 :(得分:0)

根据此处的示例: https://www.npmjs.com/package/easy-soap-request

您需要在标题中设置内容类型

const url = 'https://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php';
const headers = {
  'user-agent': 'sampleTest',
  'Content-Type': 'text/xml;charset=UTF-8',
  'soapAction': 'https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl#LatLonListZipCode',
};

这是一个对我有用的完整示例。也许是您的肥皂信封有问题?

const soapRequest = require('easy-soap-request');                          const fs = require('fs');                                                  // example data                                                            const url = 'https://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php';
const headers = {
  'user-agent': 'sampleTest',
  'Content-Type': 'text/xml;charset=UTF-8',
  'soapAction': 'https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl#LatLonListZipCode',                                                      };                                                                         
const xml = `<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>                                                            <ns3591:NDFDgen xmlns:ns3591="uri:DWMLgen">
<latitude xsi:type="xsd:string">38.99</latitude>
<longitude xsi:type="xsd:string">-77.01</longitude>                        <product xsi:type="xsd:string">time-series</product>
<startTime xsi:type="xsd:string">2019-07-04T00:00:00</startTime>
<endTime xsi:type="xsd:string">2019-07-06T00:00:00</endTime>
<Unit xsi:type="xsd:string">e</Unit>
<weatherParameters>                                                       
 <maxt xsi:type="xsd:boolean">1</maxt>
<mint xsi:type="xsd:boolean">0</mint>                                      <temp xsi:type="xsd:boolean">0</temp>                                      <td xsi:type="xsd:boolean">0</td>                                          <pop12 xsi:type="xsd:boolean">0</pop12>                                    <qpf xsi:type="xsd:boolean">0</qpf>                                        <sky xsi:type="xsd:boolean">0</sky>                                        <snow xsi:type="xsd:boolean">0</snow>                                      <wspd xsi:type="xsd:boolean">0</wspd>
<wdir xsi:type="xsd:boolean">0</wdir>
<wx xsi:type="xsd:boolean">0</wx>
<waveh xsi:type="xsd:boolean">0</waveh>
<icons xsi:type="xsd:boolean">0</icons>
<critfireo xsi:type="xsd:boolean">0</critfireo>
<dryfireo xsi:type="xsd:boolean">0</dryfireo>
<rhm xsi:type="xsd:boolean">0</rhm>
<apt xsi:type="xsd:boolean">0</apt>
<tcwspdabv34i xsi:type="xsd:boolean">0</tcwspdabv34i>
<tcwspdabv50i xsi:type="xsd:boolean">0</tcwspdabv50i>
<tcwspdabv64i xsi:type="xsd:boolean">0</tcwspdabv64i>
<tcwspdabv34c xsi:type="xsd:boolean">0</tcwspdabv34c>
<tcwspdabv50c xsi:type="xsd:boolean">0</tcwspdabv50c>
<tcwspdabv64c xsi:type="xsd:boolean">0</tcwspdabv64c>
<conhazo xsi:type="xsd:boolean">0</conhazo>
<ptornado xsi:type="xsd:boolean">0</ptornado>
<phail xsi:type="xsd:boolean">0</phail>
<ptstmwinds xsi:type="xsd:boolean">0</ptstmwinds>
<pxtornado xsi:type="xsd:boolean">0</pxtornado>
<pxhail xsi:type="xsd:boolean">0</pxhail>
<pxtstmwinds xsi:type="xsd:boolean">0</pxtstmwinds>
<ptotsvrtstm xsi:type="xsd:boolean">0</ptotsvrtstm>
<ptotxsvrtstm xsi:type="xsd:boolean">0</ptotxsvrtstm>
<tmpabv14d xsi:type="xsd:boolean">0</tmpabv14d>
<tmpblw14d xsi:type="xsd:boolean">0</tmpblw14d>
<tmpabv30d xsi:type="xsd:boolean">0</tmpabv30d>
<tmpblw30d xsi:type="xsd:boolean">0</tmpblw30d>
<tmpabv90d xsi:type="xsd:boolean">0</tmpabv90d>
<tmpblw90d xsi:type="xsd:boolean">0</tmpblw90d>
<prcpabv14d xsi:type="xsd:boolean">0</prcpabv14d>
<prcpblw14d xsi:type="xsd:boolean">0</prcpblw14d>
<prcpabv30d xsi:type="xsd:boolean">0</prcpabv30d>
<prcpblw30d xsi:type="xsd:boolean">0</prcpblw30d>
<prcpabv90d xsi:type="xsd:boolean">0</prcpabv90d>
<prcpblw90d xsi:type="xsd:boolean">0</prcpblw90d>
<precipa_r xsi:type="xsd:boolean">0</precipa_r>
<sky_r xsi:type="xsd:boolean">0</sky_r>
<td_r xsi:type="xsd:boolean">0</td_r>
<temp_r xsi:type="xsd:boolean">0</temp_r>
<wdir_r xsi:type="xsd:boolean">0</wdir_r>
<wspd_r xsi:type="xsd:boolean">0</wspd_r>                                  
<wgust xsi:type="xsd:boolean">0</wgust>
<iceaccum xsi:type="xsd:boolean">0</iceaccum>                              
<maxrh xsi:type="xsd:boolean">0</maxrh>                                    
<minrh xsi:type="xsd:boolean">0</minrh>                                    
</weatherParameters>                                                       
</ns3591:NDFDgen>                                                          
</SOAP-ENV:Body>                                                           
</SOAP-ENV:Envelope>`;                                                     
(async () => {
  const { response } = await soapRequest(url, headers, xml, 4000); // Optional timeout parameter(milliseconds)
  const { respHeaders, body, statusCode } = response;                        
console.log(respHeaders);                                                  
console.log(body);                                                         
console.log(statusCode);
})();