Swift
Swift
HTTP SOAP 1.1 Request and Response using POST
See more HTTP Examples
Demonstrates a working SOAP 1.1 request and response using POST with a live server. You may try running this example with the URLs and data provided. See http://secure.smartbearsoftware.com/samples/testcomplete10/webservices/Service.asmx?WSDL for detailsChilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// --------------------------------------------------------------------------------
// Also see Chilkat's Online WSDL Code Generator
// to generate code and SOAP Request and Response XML for each operation in a WSDL.
// --------------------------------------------------------------------------------
let http = CkoHttp()!
// Generate the following XML:
// <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:smar="http://smartbear.com">
// <soapenv:Header/>
// <soapenv:Body>
// <smar:HelloWorld/>
// </soapenv:Body>
// </soapenv:Envelope>
let soapXml = CkoXml()!
soapXml.tag = "soapenv:Envelope"
soapXml.addAttribute(name: "xmlns:soapenv", value: "http://schemas.xmlsoap.org/soap/envelope/")
soapXml.addAttribute(name: "xmlns:smar", value: "http://smartbear.com")
soapXml.updateChildContent(tagPath: "soapenv:Header", value: "")
soapXml.updateChildContent(tagPath: "soapenv:Body|smar:HelloWorld", value: "")
print("\(soapXml.getXml()!)")
let req = CkoHttpRequest()!
req.httpVerb = "POST"
req.sendCharset = false
req.addHeader(name: "Content-Type", value: "text/xml; charset=utf-8")
req.addHeader(name: "SOAPAction", value: "http://smartbear.com/HelloWorld")
req.path = "/samples/testcomplete10/webservices/Service.asmx"
success = req.loadBody(fromString: soapXml.getXml(), charset: "utf-8")
http.followRedirects = true
let resp = CkoHttpResponse()!
success = http.httpSReq(domain: "secure.smartbearsoftware.com", port: 443, ssl: true, request: req, response: resp)
if success == false {
print("\(http.lastErrorText!)")
return
}
let xmlResponse = CkoXml()!
success = xmlResponse.load(xmlData: resp.bodyStr)
print("\(xmlResponse.getXml()!)")
// A successful XML response:
// <?xml version="1.0" encoding="utf-8" ?>
// <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
// <soap:Body>
// <HelloWorldResponse xmlns="http://smartbear.com">
// <HelloWorldResult>Hello World</HelloWorldResult>
// </HelloWorldResponse>
// </soap:Body>
// </soap:Envelope>
}