Sample code for 30+ languages & platforms
Swift

Yet Another SOAP MTOM POST Example

See more HTTP Misc Examples

Demonstrates how to sending a SOAP request with an MTOM/XOP attachment...

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let http = CkoHttp()!

    // First build the following XML:

    // 	<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    // 	    xmlns:imp="http://www.yourdomain.it/ImportMultiploWorkflowPAWS/">
    // 	   <soapenv:Header/>
    // 	   <soapenv:Body>
    // 	      <imp:InputImportMultiploWorkflowPA>
    // 	         <usernameEis>MyUsername</usernameEis>
    // 	         <passwordEis>MyPassword</passwordEis>
    // 	         <postazioneEis>COSO00</postazioneEis>
    // 	         <istitutoEis>COSO</istitutoEis>
    // 	         <codiceAzienda>COSO0000</codiceAzienda>
    // 	         <fileZip><inc:Include href="cid:780931946797" xmlns:inc="http://www.w3.org/2004/08/xop/include"/></fileZip>
    // 	         <fileZipName>IT04769180151_00044.zip</fileZipName>
    // 	         <workflow>carica</workflow>
    // 	      </imp:InputImportMultiploWorkflowPA>
    // 	   </soapenv:Body>
    // 	</soapenv:Envelope>

    let xml = CkoXml()!
    xml.tag = "soapenv:Envelope"
    xml.addAttribute(name: "xmlns:soapenv", value: "http://schemas.xmlsoap.org/soap/envelope/")
    xml.addAttribute(name: "xmlns:imp", value: "http://www.yourdomain.it/ImportMultiploWorkflowPAWS/")
    xml.updateChildContent(tagPath: "soapenv:Header", value: "")
    xml.updateChildContent(tagPath: "soapenv:Body|imp:InputImportMultiploWorkflowPA|usernameEis", value: "MyUsername")
    xml.updateChildContent(tagPath: "soapenv:Body|imp:InputImportMultiploWorkflowPA|passwordEis", value: "MyPassword")
    xml.updateChildContent(tagPath: "soapenv:Body|imp:InputImportMultiploWorkflowPA|postazioneEis", value: "COSO00")
    xml.updateChildContent(tagPath: "soapenv:Body|imp:InputImportMultiploWorkflowPA|istitutoEis", value: "COSO")
    xml.updateChildContent(tagPath: "soapenv:Body|imp:InputImportMultiploWorkflowPA|codiceAzienda", value: "COSO0000")
    xml.updateAttrAt(tagPath: "soapenv:Body|imp:InputImportMultiploWorkflowPA|fileZip|inc:Include", autoCreate: true, attrName: "href", attrValue: "cid:780931946797")
    xml.updateAttrAt(tagPath: "soapenv:Body|imp:InputImportMultiploWorkflowPA|fileZip|inc:Include", autoCreate: true, attrName: "xmlns:inc", attrValue: "http://www.w3.org/2004/08/xop/include")
    xml.updateChildContent(tagPath: "soapenv:Body|imp:InputImportMultiploWorkflowPA|fileZipName", value: "IT04769180151_00044.zip")
    xml.updateChildContent(tagPath: "soapenv:Body|imp:InputImportMultiploWorkflowPA|workflow", value: "carica")

    // We want our top-level HTTP request header to look like this:

    // POST /http-router-softwarehubsystem/webservices/importMultiploWorkflowPA HTTP/1.1
    // Accept-Encoding: gzip,deflate
    // Content-Type: multipart/related; type="application/xop+xml"; start="<rootpart@soapui.org>"; start-info="text/xml"; boundary="----=_Part_0_1353857996.1546013984933"
    // SOAPAction: "http://www.yourdomain.it/ImportMultiploWorkflowPAWS/importMultiploWorkflowPA"
    // MIME-Version: 1.0
    // Content-Length: <chilkat will add this header>
    // Host: <chilkat will add this header>
    // Connection: Keep-Alive
    // User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

    let req = CkoHttpRequest()!
    req.httpVerb = "POST"
    req.path = "/http-router-softwarehubsystem/webservices/importMultiploWorkflowPA"
    req.contentType = "multipart/related; type=\"application/xop+xml\"; start=\"<rootpart@soapui.org>\"; start-info=\"text/xml\""
    req.addHeader(name: "SOAPAction", value: "\"http://www.yourdomain.it/ImportMultiploWorkflowPAWS/importMultiploWorkflowPA\"")
    req.addHeader(name: "Accept-Encoding", value: "gzip,deflate")
    req.addHeader(name: "Mime-Version", value: "1.0")
    req.addHeader(name: "Connection", value: "Keep-Alive")
    req.addHeader(name: "User-Agent", value: "Apache-HttpClient/4.1.1 (java 1.5)")

    // Add the XML content for the 1st MIME sub-part.
    xml.emitXmlDecl = false
    xml.emitCompact = false
    success = req.addString(forUpload2: "", filename: "", strData: xml.getXml(), charset: "utf-8", contentType: "application/xop+xml; charset=UTF-8; type=\"text/xml\"")

    // We want the 1st sub-header (the sub-part containing the XML) to look like this:

    // Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
    // Content-Transfer-Encoding: 8bit
    // Content-ID: <rootpart@soapui.org>

    // Make sure we set sub-headers *after* calling AddStringForUpload2, otherwise AddStringForUpload2 might change these explicitly set headers.
    success = req.addSubHeader(index: 0, name: "Content-Transfer-Encoding", value: "8bit")
    success = req.addSubHeader(index: 0, name: "Content-ID", value: "<rootpart@soapui.org>")

    // Add a reference to the local file for the file data for the 2nd MIME sub-part.
    success = req.addFile(forUpload2: "", path: "qa_data/zips/helloWorld.zip", contentType: "application/zip; name=IT04769180151_00044.zip")

    // We want the 2nd sub-header (for the MIME sub-part containing a .zip file) to look like this:
    // Content-Type: application/zip; name=IT04769180151_00044.zip
    // Content-Transfer-Encoding: binary
    // Content-ID: <IT04769180151_00044.zip>
    // Content-Disposition: attachment; name="IT04769180151_00044.zip"; filename="IT04769180151_00044.zip"

    // Make sure we set sub-headers *after* calling AddFileForUpload2, otherwise AddFileForUpload2 might change these explicitly set headers.
    success = req.addSubHeader(index: 1, name: "Content-Transfer-Encoding", value: "binary")
    success = req.addSubHeader(index: 1, name: "Content-ID", value: "<IT04769180151_00044.zip>")
    success = req.addSubHeader(index: 1, name: "Content-Disposition", value: "attachment; name=\"IT04769180151_00044.zip\"; filename=\"IT04769180151_00044.zip\"")

    // Keep a session log so we can examine the exact request/response.
    // Remove this line when finished with debugging.
    http.sessionLogFilename = "qa_output/mtom_sessionLog.txt"

    var useTls: Bool = false
    // Note: Please don't run this example without changing the domain to your own domain...
    let resp = CkoHttpResponse()!
    success = http.httpSReq(domain: "www.yourdomain.it", port: 80, ssl: useTls, request: req, response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    let xmlResponse = CkoXml()!
    success = xmlResponse.load(xmlData: resp.bodyStr)
    // println xmlResponse.GetXml();

    print("OK.")

}