JcMultiPartContent
public struct JcMultiPartContent
Convenience class to allow multi-part formatted data to be sent/received via http/s
Example: (get)
return super._getMultipart(resourcePath: args(id)) { (response) in
if (response.status == .SUCCESS) {
for part in response.content!.parts {
var name = part.name
var filename = part.fileName
var contentType = part.contentType
var data = part.content
}
}
}
Example: (post)
var mp = JcMultiPartContent()
mp.add("object", contentType: nil, content: Data("{\"name\": \"\(name)\", \"type\":\"\(contentType)\"}".utf8))
mp.add("filesize", contentType: nil, content: Data("\(data.count)".utf8))
mp.add("file", contentType: contentType, content: data)
return super._execute(method: JcConnectionRequest.Method.POST, resourcePath: C8Y_BINARIES_API, request: mp) { (response) in
if (response.status == .SUCCESS) {
...
}
}
-
The content parts to be sent or have been received
Declaration
Swift
public private(set) var parts: [ContentPart] { get }
-
Defines the content part
See moreDeclaration
Swift
public struct ContentPart : Encodable
-
Create a new multipart instance, use one of the
add()
functions to add content partsDeclaration
Swift
public init()
-
Adds a content part for the givent data
Declaration
Swift
public mutating func add(_ name: String, contentType: String?, content: Data)
Parameters
name
name of the content part
contentType
Defines the type of data e.g. ‘applicaton/jpeg’ etc.
content
data to be included
-
Adds a content part for the givent data including a unique id
Declaration
Swift
public mutating func add(withId: String, name: String, contentType: String?, content: Data)
Parameters
id
Unique id to be included
name
name of the content part
contentType
Defines the type of data e.g. ‘applicaton/jpeg’ etc.
content
data to be included
-
Generates raw multipart output that can then be used as a request to a
URLSession
callAttention:
You will need to set the content type to include the multipart boundary type If not using the
ConnectionRequest
class. Namely #multipart/form-data; boundary=fileBoundary#Declaration
Swift
public func build() -> Data