Swift
Swift
Set a Multipart Part Body from a StringBuilder
See more REST Examples
Demonstrates Rest.SetMultipartBodySb, which sets the text body of the selected multipart part from a StringBuilder.
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
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: "application/json")
// Set the text body of the selected part from a StringBuilder.
let sbBody = CkoStringBuilder()!
sbBody.append(value: "{ \"key\": \"value\" }")
success = rest.setMultipartBodySb(bodySb: sbBody)
if success == false {
print("\(rest.lastErrorText!)")
return
}
var responseText: String? = rest.fullRequestMultipart(httpVerb: "POST", uriPath: "/api/upload")
if rest.lastMethodSuccess == false {
print("\(rest.lastErrorText!)")
return
}
print("\(responseText!)")
}