Skip to content

Instantly share code, notes, and snippets.

@chrillo
Created April 15, 2012 13:19
Show Gist options
  • Save chrillo/2392759 to your computer and use it in GitHub Desktop.
Save chrillo/2392759 to your computer and use it in GitHub Desktop.
SoapHeaders
Client.prototype.addSoapHeader=function(ns,tag,value){
if(!this.soapHeaders){
this.soapHeaders=[];
}
this.soapHeaders.push({ns:ns,tag:tag,value:value});
}
Client.prototype._getHeaders=function(){
if(!this.soapHeaders){
return;
}
var headers="";
this.soapHeaders.forEach(function(header){
headers+="<m:"+header.tag+" xmlns:m=\""+header.ns+"\">"+header.value+"</m:"+header.tag+">";
})
return headers;
}
Client.prototype._invoke = function(method, arguments, location, callback) {
var self = this,
name = method.$name,
input = method.input,
output = method.output,
style = method.style,
defs = this.wsdl.definitions,
ns = defs.$targetNamespace,
encoding = '',
message = '',
xml = null,
headers = {
SOAPAction: ((ns.lastIndexOf("/") != ns.length - 1) ? ns + "/" : ns) + name,
'Content-Type': "text/xml; charset=utf-8"
},
options = {};
// Allow the security object to add headers
if (self.security && self.security.addHeaders)
self.security.addHeaders(headers);
if (self.security && self.security.addOptions)
self.security.addOptions(options);
if (input.parts) {
assert.ok(!style || style == 'rpc', 'invalid message definition for document style binding');
message = self.wsdl.objectToRpcXML(name, arguments);
encoding = 'soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" ';
}
else {
assert.ok(!style || style == 'document', 'invalid message definition for rpc style binding');
message = self.wsdl.objectToDocumentXML(input.$name, arguments);
}
xml = "<soap:Envelope " +
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
encoding +
"xmlns:ns0=\""+ns+"\">" +
"<soap:Header>" +
//(self.security ? self.security.toXML() : "") +
self._getHeaders()+
"</soap:Header>" +
"<soap:Body>" +
message +
"</soap:Body>" +
"</soap:Envelope>";
http.request(location, xml, function(err, response, body) {
if (err) {
callback(err);
}
else {
try {
var obj = self.wsdl.xmlToObject(body);
}
catch (error) {
callback(error, null, body);
return;
}
callback(null, obj[output.$name], body);
}
}, headers, options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment