diff --git a/ballerina/soap.bal b/ballerina/soap.bal index c12a4b9..4629efa 100644 --- a/ballerina/soap.bal +++ b/ballerina/soap.bal @@ -59,9 +59,10 @@ public isolated client class Client { # # + body - SOAP request body as an `XML` or `mime:Entity[]` to work with SOAP attachments # + action - SOAP action as a `string` + # + headers - SOAP headers as a `map` # + return - If successful, returns the response. Else, returns an error - remote function sendReceive(xml|mime:Entity[] body, string action) returns xml|mime:Entity[]|Error { - return sendReceive(self.soapVersion, body, self.soapClient, action); + remote function sendReceive(xml|mime:Entity[] body, string action, map headers = {}) returns xml|mime:Entity[]|Error { + return sendReceive(self.soapVersion, body, self.soapClient, action, headers); } # Fires and forgets requests. Sends the request without the possibility of any response from the @@ -72,8 +73,9 @@ public isolated client class Client { # # + body - SOAP request body as an `XML` or `mime:Entity[]` to work with SOAP attachments # + action - SOAP action as a `string` + # + headers - SOAP headers as a `map` # + return - If successful, returns `nil`. Else, returns an error - remote function sendOnly(xml|mime:Entity[] body, string action) returns Error? { - return sendOnly(self.soapVersion, body, self.soapClient, action); + remote function sendOnly(xml|mime:Entity[] body, string action, map headers = {}) returns Error? { + return sendOnly(self.soapVersion, body, self.soapClient, action, headers); } } diff --git a/ballerina/soap_utils.bal b/ballerina/soap_utils.bal index b608f46..7567ff5 100644 --- a/ballerina/soap_utils.bal +++ b/ballerina/soap_utils.bal @@ -22,8 +22,9 @@ import ballerina/mime; # + soapAction - SOAP action # + body - SOAP request body as an `XML` or `mime:Entity[]` to work with soap attachments # + soapVersion - The SOAP version of the request +# + headers - SOAP headers as a `map` # + return - The SOAP Request sent as `http:Request` -function createHttpRequest(SoapVersion soapVersion, xml|mime:Entity[] body, string soapAction) +function createHttpRequest(SoapVersion soapVersion, xml|mime:Entity[] body, string soapAction, map headers = {}) returns http:Request { http:Request req = new; if body is xml { @@ -43,6 +44,10 @@ returns http:Request { req.setHeader(mime:CONTENT_TYPE, mediaType.toString()); } } + foreach string key in headers.keys() { + req.addHeader(key, headers[key].toBalString()); + } + return req; } @@ -61,8 +66,8 @@ function createSoapResponse(http:Response response, SoapVersion soapVersion) ret string path = ""; -function sendReceive(SoapVersion soapVersion, xml|mime:Entity[] body, http:Client httpClient, string soapAction) returns xml|Error { - http:Request req = createHttpRequest(soapVersion, body, soapAction); +function sendReceive(SoapVersion soapVersion, xml|mime:Entity[] body, http:Client httpClient, string soapAction, map headers = {}) returns xml|Error { + http:Request req = createHttpRequest(soapVersion, body, soapAction, headers); http:Response response; do { response = check httpClient->post(path, req); @@ -76,8 +81,8 @@ function sendReceive(SoapVersion soapVersion, xml|mime:Entity[] body, http:Clien } } -function sendOnly(SoapVersion soapVersion, xml|mime:Entity[] body, http:Client httpClient, string soapAction) returns Error? { - http:Request req = createHttpRequest(SOAP11, body, soapAction); +function sendOnly(SoapVersion soapVersion, xml|mime:Entity[] body, http:Client httpClient, string soapAction, map headers = {}) returns Error? { + http:Request req = createHttpRequest(SOAP11, body, soapAction, headers); do { http:Response _ = check httpClient->post(path, req); } on fail var err { diff --git a/ballerina/tests/basic_client_test.bal b/ballerina/tests/basic_client_test.bal index 79fefbe..db15011 100644 --- a/ballerina/tests/basic_client_test.bal +++ b/ballerina/tests/basic_client_test.bal @@ -94,3 +94,45 @@ function testSendOnly12() returns error? { _ = check soapClient->sendOnly(body, "http://tempuri.org/Add"); } + +@test:Config {} +function testSendReceive11WithHeaders() returns error? { + Client soapClient = check new ("http://www.dneonline.com/calculator.asmx?WSDL"); + + xml body = xml ` + + + 2 + 3 + + + `; + + xml|mime:Entity[] response = check soapClient->sendReceive(body, "http://tempuri.org/Add", {foo: ["bar1", "bar2"]}); + + xml expected = xml `5`; + test:assertEquals(response, expected); +} + +@test:Config {} +function testSendReceive12WithHeaders() returns error? { + Client soapClient = check new ("http://www.dneonline.com/calculator.asmx?WSDL", soapVersion = SOAP12); + + xml body = xml ` + + + 2 + 3 + + + `; + + xml|mime:Entity[] response = check soapClient->sendReceive(body, "http://tempuri.org/Add", {foo: ["bar1", "bar2"]}); + + xml expected = xml `5`; + test:assertEquals(response, expected); +}