Sample code for 30+ languages & platforms
Swift

Set a Multipart Part Body from a String

See more REST Examples

Demonstrates Rest.SetMultipartBodyString, which sets the text body of the currently selected multipart part.

Background. A multipart request is assembled part by part: the part selector chooses the part being built, header fields describe it, and a SetMultipartBody method supplies its content. The request is then sent with a multipart send or full-request method.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    let rest = CkoRest()!
    var bTls: Bool = true
    var bAutoReconnect: Bool = true
    success = rest.connect(hostname: "example.com", port: 443, tls: bTls, autoReconnect: bAutoReconnect)
    if success == false {
        print("\(rest.lastErrorText!)")
        return
    }

    //  Select the multipart part to build, and set its header fields.
    rest.partSelector = "1"
    rest.addHeader(name: "Content-Disposition", value: "form-data; name=\"field1\"")
    rest.addHeader(name: "Content-Type", value: "text/plain")

    //  Set the text body of the selected multipart part.
    success = rest.setMultipartBodyString(bodyText: "value1")
    if success == false {
        print("\(rest.lastErrorText!)")
        return
    }

    //  Send the multipart request using the configured part(s).
    var responseText: String? = rest.fullRequestMultipart(httpVerb: "POST", uriPath: "/api/upload")
    if rest.lastMethodSuccess == false {
        print("\(rest.lastErrorText!)")
        return
    }

    print("\(responseText!)")

}