Skip to content

Instantly share code, notes, and snippets.

@liudonghua123
Created May 31, 2019 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liudonghua123/3d96acab29ce8d627feeb9c8858a38ea to your computer and use it in GitHub Desktop.
Save liudonghua123/3d96acab29ce8d627feeb9c8858a38ea to your computer and use it in GitHub Desktop.
A simple node app for soap operation demonstration
const getCircularReplacer = () => {
const seen = new WeakSet();
return (key, value) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
};
const fs = require("fs");
const soap = require('soap');
const xml2js = require('xml2js');
const parser = new xml2js.Parser();
(async () => {
const url = 'https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl';
let args = {LatLonListZipCode: 75001};
const client = await soap.createClientAsync(url);
//client.describe();
//console.info(`client: ${JSON.stringify(client, getCircularReplacer(), 2)}`);
// fs.writeFile("temp.txt", JSON.stringify(client, getCircularReplacer(), 2), (err) => {
// if (err) console.log(err);
// console.log("Successfully Written to File.");
// });
const [result] = await client.LatLonListZipCodeAsync(args);
console.log(`result: ${JSON.stringify(result, getCircularReplacer(), 2)}`);
parser.parseString(result.listLatLonOut.$value, (err, result) => {
console.dir(err);
console.dir(result.dwml.latLonList);
})
})()
@liudonghua123
Copy link
Author

// another example use easy-soap-request
// see more on https://medium.com/@caleblemoine/how-to-perform-soap-requests-with-node-js-4a9627070eb6

// easy-soap-request-test.js
const soapRequest = require('easy-soap-request');

// 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 = `
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ndf="https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl">
   <soapenv:Header/>
   <soapenv:Body>
      <ndf:LatLonListZipCode soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <zipCodeList xsi:type="dwml:zipCodeListType" xmlns:dwml="https://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd">75001</zipCodeList>
      </ndf:LatLonListZipCode>
   </soapenv:Body>
</soapenv:Envelope>
`;

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

@liudonghua123
Copy link
Author

Another way for making soap request using strong-soap

const fs = require('fs');
const util = require('util');
const soap = require('strong-soap').soap;
const XMLHandler = soap.XMLHandler;
const xmlHandler = new XMLHandler();

const url = 'https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl';
const options = {};

soap.createClient(url, options, async (err, client) => {
  let args = { LatLonListZipCode: 75001 };
  try {
    const { result, envelope } = await client.LatLonListZipCode(args);
    //response envelope
    console.log(`Response Envelope: \n${envelope}`);
    //'result' is the response body
    console.log(`Result: \n${JSON.stringify(result)}`);

    const node = xmlHandler.jsonToXml(
      null,
      null,
      XMLHandler.createSOAPEnvelopeDescriptor('soap'),
      result
    );
    const xml = node.end({ pretty: true });
    console.log(`xml:\n${xml}`);
    const root = xmlHandler.xmlToJson(null, xml, null);
    console.log(`root:\n${util.inspect(root, { depth: null })}`);
  } catch (err) {
    // handle error
    console.error(err);
  }
});

But the result also have to transform, the XMLHandler seems not enough.

The result of the following code is:

Response Envelope:
<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 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:Body><ns1:LatLonListZipCodeResponse xmlns:ns1="https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl"><listLatLonOut xsi:type="xsd:string">&lt;?xml version=&apos;1.0&apos;?&gt;&lt;dwml version=&apos;1.0&apos; xmlns:xsd=&apos;http://www.w3.org/2001/XMLSchema&apos; xmlns:xsi=&apos;http://www.w3.org/2001/XMLSchema-instance&apos; xsi:noNamespaceSchemaLocation=&apos;https://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd&apos;&gt;&lt;latLonList&gt;32.9612,-96.8372&lt;/latLonList&gt;&lt;/dwml&gt;</listLatLonOut></ns1:LatLonListZipCodeResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
Result:
{"listLatLonOut":{"$attributes":{"$xsiType":{"type":"string","xmlns":"http://www.w3.org/2001/XMLSchema"}},"$value":"<?xml version='1.0'?><dwml version='1.0' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='https://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd'><latLonList>32.9612,-96.8372</latLonList></dwml>"}}
xml: <listLatLonOut xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="http://www.w3.org/2001/XMLSchema" xsi:type="ns1:string">&lt;?xml version='1.0'?&gt;&lt;dwml version='1.0' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='https://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd'&gt;&lt;latLonList&gt;32.9612,-96.8372&lt;/latLonList&gt;&lt;/dwml&gt;</listLatLonOut>
root: {
  listLatLonOut: {
    '$attributes': {
      '$xsiType': { type: 'string', xmlns: 'http://www.w3.org/2001/XMLSchema' }
    },
    '$value': "<?xml version='1.0'?><dwml version='1.0' " +
      "xmlns:xsd='http://www.w3.org/2001/XMLSchema' " +
      "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
      "xsi:noNamespaceSchemaLocation='https://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd'><latLonList>32.9612,-96.8372</latLonList></dwml>"
  }
}

C:\Users\Liu.D.H\Desktop\web>node strong-soap-test.js
Response Envelope:
<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 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:Body><ns1:LatLonListZipCodeResponse xmlns:ns1="https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl"><listLatLonOut xsi:type="xsd:string">&lt;?xml version=&apos;1.0&apos;?&gt;&lt;dwml version=&apos;1.0&apos; xmlns:xsd=&apos;http://www.w3.org/2001/XMLSchema&apos; xmlns:xsi=&apos;http://www.w3.org/2001/XMLSchema-instance&apos; xsi:noNamespaceSchemaLocation=&apos;https://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd&apos;&gt;&lt;latLonList&gt;32.9612,-96.8372&lt;/latLonList&gt;&lt;/dwml&gt;</listLatLonOut></ns1:LatLonListZipCodeResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
Result:
{"listLatLonOut":{"$attributes":{"$xsiType":{"type":"string","xmlns":"http://www.w3.org/2001/XMLSchema"}},"$value":"<?xml version='1.0'?><dwml version='1.0' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='https://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd'><latLonList>32.9612,-96.8372</latLonList></dwml>"}}
xml:
<listLatLonOut xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="http://www.w3.org/2001/XMLSchema" xsi:type="ns1:string">&lt;?xml version='1.0'?&gt;&lt;dwml version='1.0' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='https://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd'&gt;&lt;latLonList&gt;32.9612,-96.8372&lt;/latLonList&gt;&lt;/dwml&gt;</listLatLonOut>
root:
{
  listLatLonOut: {
    '$attributes': {
      '$xsiType': { type: 'string', xmlns: 'http://www.w3.org/2001/XMLSchema' }
    },
    '$value': "<?xml version='1.0'?><dwml version='1.0' " +
      "xmlns:xsd='http://www.w3.org/2001/XMLSchema' " +
      "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
      "xsi:noNamespaceSchemaLocation='https://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd'><latLonList>32.9612,-96.8372</latLonList></dwml>"
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment