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
fileManager
FileManager
to use for file operation, if neededboundary
String
used 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-Disposition
HTTPHeader is generated from the last path component of thefileURL
. TheContent-Type
HTTPHeader MIME type is generated by mapping thefileURL
extension to the system associated MIME type.Declaration
Swift
public mutating func add(url: URL, name: String) throws
Parameters
url
URL
of the file to encoding into the instancename
String
associated to theContent-Disposition
HTTPHeader -
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-Disposition
HTTPHeader is generated from the last path component of thefileURL
. TheContent-Type
HTTPHeader MIME type is generated by mapping thefileURL
extension to the system associated MIME type.Declaration
Swift
public mutating func add(url: URL, name: String, fileName: String, mimeType: String) throws
Parameters
url
URL
of the file to encoding into the instancename
String
associated to theContent-Disposition
HTTPHeaderfileName
String
associated to theContent-Disposition
HTTPHeadermimeType
String
associated to theContent-Type
HTTPHeader -
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
data
Data
to encoding into the instance.name
Name associated to the
Data
in theContent-Disposition
HTTPHeader.fileName
Filename associated to the
Data
in theContent-Disposition
HTTPHeader.mimeType
MIME type associated to the data in the
Content-Type
HTTPHeader. -
Declaration
Swift
enum Error : Swift.Error