Reputation: 77
I Have a SOAP API like https://example.com/TokenService?wsdl
,
i send a request to create Client in below function :
let soap = require('soap');
soap.createClient(url, function(err, client) {
console.log('err is ',err) ;
client.myFunction(input_params, function(err, result) {
console.log(result);
});
});
Then i get an error like this:
Error: ENOENT: no such file or directory, open 'https:///example.com//TokenService?wsdl'
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: 'https://example.com/TokenService?wsdl`
And my client is undefiend.
Upvotes: 1
Views: 3707
Reputation: 30675
I think you're very nearly there. Here's an example of a call you can test:
const soap = require('soap');
const url = 'http://www.webservicex.net/globalweather.asmx';
const wsdlURL = 'http://www.webservicex.net/globalweather.asmx?WSDL';
soap.createClient(wsdlURL, {endpoint: url}, function(error, client) {
if (error) {
console.error(error);
} else {
client.GetCitiesByCountry({ CountryName: 'Russian Federation'}, function(error, result) {
if (error) {
console.error(error);
process.exit(1);
}
console.log(result);
});
}
});
I think you may just need to pass two urls to the function, the service url and the WSDL url. Sometimes you don't need to do this and the WSDL will contain the links to all the operation URLs. Some calls will work if you omit the {endpoint: url } option, and some won't.
Also check the WSDL link, ensure the WSDL is valid, you can look at it in your browser, it should look like this:
http://www.webservicex.net/globalweather.asmx?WSDL
You can also download the WSDL to a file and try that:
"use strict";
const wsdlFileName = 'globalweather.wsdl';
const path = require('path');
const wsdlFile = path.join(__dirname, wsdlFileName);
const soap = require('soap');
const url = 'http://www.webservicex.net/globalweather.asmx';
soap.createClient(wsdlFile, {endpoint: url}, function(error, client) {
if (error) {
console.error(error);
} else {
client.GetCitiesByCountry({ CountryName: 'Russian Federation'}, function(error, result) {
if (error) {
console.error(error);
process.exit(1);
}
console.log(result);
});
}
});
You need to put the WSDL file in the same directory as your script.
I get can this working in a C# client, the XML POST looks like this:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<reservation xmlns="http://token.ws.web.cnpg/">
<Token_param xmlns="">
<AMOUNT>amount</AMOUNT>
<CRN>crn</CRN>
<MID>mid</MID>
<REFERALADRESS>referaladdress</REFERALADRESS>
<SIGNATURE>signature</SIGNATURE>
<TID>tid</TID>
</Token_param>
</reservation>
</s:Body>
</s:Envelope>
And the response:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:reservationResponse xmlns:ns2="http://token.ws.web.cnpg/">
<return>
<result>3</result>
<token>Security check was not successful.</token>
</return>
</ns2:reservationResponse>
</S:Body>
</S:Envelope>
I can do a POST to the address by doing the following:
var request = require('request');
var url = 'https://example.com/TokenService';
var tokenXML = `<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<reservation xmlns="http://token.ws.web.cnpg/">
<Token_param xmlns="">
<AMOUNT>amount</AMOUNT>
<CRN>crn</CRN>
<MID>mid</MID>
<REFERALADRESS>referaladdress</REFERALADRESS>
<SIGNATURE>signature</SIGNATURE>
<TID>tid</TID>
</Token_param>
</reservation>
</s:Body>
</s:Envelope>`;
console.log('Posting..')
console.log(tokenXML);
request.post(
{url: url,
body : tokenXML,
headers: {'Content-Type': 'text/xml'}
}, function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
For some reason the NPM soap library does not like this webservice! But you could fill in the values in the XML yourself, it might get you a bit further if you have the details you need to fill.
I'm getting the response:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:reservationResponse xmlns:ns2="http://token.ws.web.cnpg/">
<return>
<result>3</result>
<token>Security check was not successful.</token>
</return>
</ns2:reservationResponse>
</S:Body>
</S:Envelope>
But I don't the the username password maybe. If you can fill in these details maybe you will get a successful response. You need to do an NPM install request BTW to get the request library.
Upvotes: 4