Sample code for 30+ languages & platforms
Swift

Simple SOAP Request

See more HTTP Examples

Demonstrates how to send a simple SOAP request.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // --------------------------------------------------------------------------------
    // Also see Chilkat's Online WSDL Code Generator
    // to generate code and SOAP Request and Response XML for each operation in a WSDL.
    // --------------------------------------------------------------------------------

    // Create the following XML to be sent in the SOAP request body.

    // <soapenv:Envelope xmlns:dat="http://www.dataaccess.com/webservicesserver/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    //     <soapenv:Header/>
    //     <soapenv:Body>
    //         <dat:NumberToDollars>
    //             <dat:dNum>99.0</dat:dNum>
    //         </dat:NumberToDollars>
    //     </soapenv:Body>
    // </soapenv:Envelope>

    let xml = CkoXml()!
    xml.tag = "soapenv:Envelope"
    xml.addAttribute(name: "xmlns:dat", value: "http://www.dataaccess.com/webservicesserver/")
    xml.addAttribute(name: "xmlns:soapenv", value: "http://schemas.xmlsoap.org/soap/envelope/")
    xml.updateChildContent(tagPath: "soapenv:Header", value: "")
    xml.updateChildContent(tagPath: "soapenv:Body|dat:NumberToDollars|dat:dNum", value: "99.0")

    // In a SOAP HTTP request, including the XML declaration (<?xml version="1.0" encoding="UTF-8"?>) in the XML body is generally not required. 
    xml.emitXmlDecl = false
    var soapRequestBody: String? = xml.getXml()

    var endpoint: String? = "https://www.dataaccess.com/webservicesserver/numberconversion.wso"
    var soapAction: String? = ""
    //  For SOAP requests, the standard Content-Type is usually set to "text/xml" or "application/soap+xml"
    var contentType: String? = "text/xml"

    let http = CkoHttp()!

    http.clearHeaders()
    http.setRequestHeader(name: "SOAPAction", value: soapAction)

    let resp = CkoHttpResponse()!
    success = http.httpStr(verb: "POST", url: endpoint, bodyStr: soapRequestBody, charset: "utf-8", contentType: contentType, response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    // Get the XML response body.
    let responseXml = CkoXml()!
    resp.getBodyXml(xml: responseXml)

    var statusCode: Int = resp.statusCode.intValue
    print("response status code: \(statusCode)")

    // If the status code does not indicate succcess, then show the response XML,
    // which probably contains error information.
    if statusCode != 200 {
        print("\(responseXml.getXml()!)")
        return
    }

    print("\(responseXml.getXml()!)")

    // Parse the successful SOAP response XML.

    // This is a sample of the response XML, but the namespace prefixes will be different.
    // We can parse the result using "*" for the namespace prefixes (see below).

    // <soapenv:Envelope xmlns:dat="http://www.dataaccess.com/webservicesserver/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    //     <soapenv:Header/>
    //     <soapenv:Body>
    //         <dat:NumberToDollarsResponse>
    //             <dat:NumberToDollarsResult>string</dat:NumberToDollarsResult>
    //         </dat:NumberToDollarsResponse>
    //     </soapenv:Body>
    // </soapenv:Envelope>

    var dat_NumberToDollarsResult: String? = responseXml.getChildContent(tagPath: "*:Body|*:NumberToDollarsResponse|*:NumberToDollarsResult")
    print("result: \(dat_NumberToDollarsResult!)")

}