Creating SOAP Request using The API scripting language

Hello All,
Here is a simple SOAP Request. Used SOAPUI to see the HTTP level of the request then created code listed below;

//Import the `assert` module to validate results.
var assert = require('assert');
var SoapRequestXML='<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://www.webserviceX.NET/">\r\n' +
                   '<soap:Header/>\r\n' +
                   '   <soap:Body>\r\n' + 
                   '      <web:GetQuote>\r\n' +
                   '         <web:symbol>XRX</web:symbol>\r\n' +
                   '      </web:GetQuote>\r\n' +
                   '   </soap:Body>\r\n' +
                   '</soap:Envelope>';


var options = {
    //Define endpoint URL.
    url: "http://www.webservicex.com/stockquote.asmx",
    //Define body of POST request.
    body: SoapRequestXML,
    //Define insert key and expected data type.
    headers: {
         'Content-Type': 'application/soap+xml;charset=UTF-8;action="http://www.webserviceX.NET/GetQuote"',
         'Host': 'www.webservicex.net',
         'Accept-Encoding': 'gzip,deflate',
         'Connection': 'Keep-Alive'
         
        }
};

//Define expected results using callback function.
function callback(error, response, body) {
    //Log status code to Synthetics console.
    console.log(response.statusCode + " status code");
    //Verify endpoint returns 200 (OK) response code.
    assert.ok(response.statusCode == 200, 'Expected 200 OK response');
    //Parse JSON received from Insights into variable.
    //
   var parseString = require('xml2js').parseString;
   var XMLReSULT = response.body;
   parseString(XMLReSULT, function (err, result) {
    console.dir(result);
     
   });
   
    //Log end of script.
    console.log("End reached");
}

//Make POST request, passing in options and callback.
$http.post(options, callback);
7 Likes

I hacked something together for interacting with SOAP API’s that’s similar but yours is a bit more elegant.

One thing I used to aid in diagnostics is to use JSON.Stringify() function to log the response to our synthetics script console:

parseXML(body, function (err, resultJSON) {
          console.log('Parsing XML to JSON');
          console.log('XML to JSON Result: ' + JSON.stringify(resultJSON));

Does yours do something similar with parseString? I was trying that but I guess I was invoking it wrong because it never worked.

<\Joe LoCascio>
1 Like

I am using below code, using @mosowski code. But, I am getting The message with Action ‘’ cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).</

Can someone help?

//Import the assert module to validate results.
var assert = require(‘assert’);
var SoapRequestXML = ‘<soapenv:Envelope xmlns:soapenv=“http://schemas.xmlsoap.org/soap/envelope/” xmlns:ns=“http://theretailequation.com/verify2ws/1.1” xmlns:tre=“http://schemas.datacontract.org/2004/07/TRE.Verify2.WebService.V1_1”>’ +
soapenv:Header/’ +
soapenv:Body’ +
ns:Echo’ +
ns:echoRequest’ +
tre:EchoData’ +
tre:EchoDataStringfsdf?</tre:EchoDataString>’ +
‘</tre:EchoData>’ +
tre:MessageHeader’ +
tre:AssociateId1122</tre:AssociateId>’ +
tre:ErrorCode?</tre:ErrorCode>’ +
tre:ErrorFlag0</tre:ErrorFlag>’ +
tre:ErrorMessage?</tre:ErrorMessage>’ +
tre:PosTerminalId1122</tre:PosTerminalId>’ +
tre:RetailerId21</tre:RetailerId>’ +
tre:RetailerTransactionId20170907-1-4</tre:RetailerTransactionId>’ +
tre:RouteNumber530</tre:RouteNumber>’ +
tre:StoreNumber8131</tre:StoreNumber>’ +
tre:TransactionTime20170330010101</tre:TransactionTime>’ +
tre:TransactionType1</tre:TransactionType>’ +
‘</tre:MessageHeader>’ +
‘</ns:echoRequest>’ +
‘</ns:Echo>’ +
‘</soapenv:Body>’ +
‘</soapenv:Envelope>’;

var options = {
//Define endpoint URL.
url: “https://tre.verifywebsvctest.theretailequation.com/verify2webservice/1.1/TreVerify.svc”,
//Define body of POST request.
body: SoapRequestXML,
//Define insert key and expected data type.
headers: {
‘Content-Type’: ‘text/xml;charset=UTF-8;action=“http://theretailequation.com/verify2ws/1.1/ITreVerify/Echo”’,
‘Accept-Encoding’: ‘gzip,deflate’,
‘Connection’: ‘Keep-Alive’
}
};

//Define expected results using callback function.
function callback(error, response, body) {
//Log status code to Synthetics console.
console.log(response);
console.log(response.statusCode + " status code");
//Verify endpoint returns 200 (OK) response code.
assert.ok(response.statusCode == 200, ‘Expected 200 OK response’);
//Parse JSON received from Insights into variable.
var parseString = require(‘xml2js’).parseString;
var XMLReSULT = response.body;
parseString(XMLReSULT, function (err, result) {
console.dir(result);

});

//Log end of script.
console.log("End reached");

}

//Make POST request, passing in options and callback.
$http.post(options, callback);

Thanks for the question @sranade. It’s been a while @jlocascio - any ideas for this new question?