Sample code for 30+ languages & platforms
Swift

REST File Upload (multipart/form-data)

See more REST Examples

Demonstrates how to upload a file using multipart/form-data.

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 rest = CkoRest()!

    // Connect to the HTTP server using TLS.
    var bTls: Bool = true
    var port: Int = 443
    var bAutoReconnect: Bool = true
    // Make sure to replace "www.chilkatsoft.com" with your domain..
    success = rest.connect(hostname: "www.chilkatsoft.com", port: port, tls: bTls, autoReconnect: bAutoReconnect)
    if success != true {
        print("\(rest.lastErrorText!)")
        return
    }

    let fileStream = CkoStream()!
    fileStream.sourceFile = "qa_data/jpg/starfish.jpg"

    rest.addHeader(name: "Content-Type", value: "multipart/form-data")

    rest.partSelector = "1"
    rest.addHeader(name: "Content-Type", value: "image/jpg")
    rest.addHeader(name: "Content-Disposition", value: "form-data; name=\"filename\"; filename=\"starfish.jpg\"")
    rest.setMultipartBodyStream(stream: fileStream)
    rest.partSelector = "0"

    var responseBody: String? = rest.fullRequestMultipart(httpVerb: "POST", uriPath: "/the_uri_path_to_receive_the_upload")
    if rest.lastMethodSuccess != true {
        print("\(rest.lastErrorText!)")
        return
    }

    if rest.responseStatusCode.intValue != 200 {
        print("Received error response code: \(rest.responseStatusCode.intValue)")
        return
    }

    print("File uploaded")

}