MultipartFormData
public struct MultipartFormData
Constructs multipart/form-data for uploads within an HTTP or HTTPS body.
We encode the data directly in memory. It’s very efficient, but can lead to memory issues if the dataset is too large (eg: a Video)
Warning: A Second approch to encode bigger dataset will be addes later
-
Creates an instance
Declaration
Swift
public init(fileManager: FileManager = .default, boundary: String? = nil)Parameters
fileManagerFileManagerto use for file operation, if neededboundaryStringused to separate body parts -
Creates a body part from the file and add it to the instance
The body part data will be encode by using this format:
Content-Disposition: form-data; name=#{name}; filename=#{generated filename}(HTTPHeader)Content-Type: #{generated mimeType}(HTTPHeader)- Encoded file data
- Multipart form boundary
The filename in the
Content-DispositionHTTPHeader is generated from the last path component of thefileURL. TheContent-TypeHTTPHeader MIME type is generated by mapping thefileURLextension to the system associated MIME type.Declaration
Swift
public mutating func add(url: URL, name: String) throwsParameters
urlURLof the file to encoding into the instancenameStringassociated to theContent-DispositionHTTPHeader -
Creates a body part from the file and add it to the instance
The body part data will be encode by using this format:
Content-Disposition: form-data; name=#{name}; filename=#{generated filename}(HTTPHeader)Content-Type: #{generated mimeType}(HTTPHeader)- Encoded file data
- Multipart form boundary
The filename in the
Content-DispositionHTTPHeader is generated from the last path component of thefileURL. TheContent-TypeHTTPHeader MIME type is generated by mapping thefileURLextension to the system associated MIME type.Declaration
Swift
public mutating func add(url: URL, name: String, fileName: String, mimeType: String) throwsParameters
urlURLof the file to encoding into the instancenameStringassociated to theContent-DispositionHTTPHeaderfileNameStringassociated to theContent-DispositionHTTPHeadermimeTypeStringassociated to theContent-TypeHTTPHeader -
Creates a body part from the data and add it to the instance.
The body part data will be encoded by using this:
Content-Disposition: form-data; name=#{name}; filename=#{filename}(HTTPHeader)Content-Type: #{mimeType}(HTTPHeader)- Encoded file data
Multipart form boundary
Declaration
Swift
public mutating func add(data: Data, name: String, fileName: String? = nil, mimeType: String? = nil)Parameters
dataDatato encoding into the instance.nameName associated to the
Datain theContent-DispositionHTTPHeader.fileNameFilename associated to the
Datain theContent-DispositionHTTPHeader.mimeTypeMIME type associated to the data in the
Content-TypeHTTPHeader. -
Declaration
Swift
enum Error : Swift.Error
MultipartFormData Structure Reference