Swift
Swift
Set a Multipart Part Body from BinData
See more REST Examples
Demonstrates Rest.SetMultipartBodyBd, which sets the binary body of the selected multipart part from a BinData.
The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
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
}
// The file paths are relative to the application's current working directory. Absolute paths
// may also be used. Supply the paths appropriate to your own environment.
// 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: "image/png")
// Set the binary body of the selected part from a BinData.
let bdBody = CkoBinData()!
var bLoaded: Bool = bdBody.loadFile(path: "qa_data/image.png")
if bLoaded != true {
print("Failed to load the part body file.")
return
}
success = rest.setMultipartBodyBd(bodyData: bdBody)
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!)")
}