You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add a convenience for multi-part form data in an HTTP body, the bane of many iOS developers' existence when integrating with APIs. Here's an example of how it could look:
/// An individual item in a multipart/form-data request.
structNetworkFormData{
/// The key associated with this item. For example, if you see `key=value` in sample documentation, this represents the left side of that assignment.
letkey:String
/// The actual data to encode as the value.
letdata:Data
/// The MIME type of the data.
letmimeType:String
/// The name of the file being uploaded. Will be excluded if `nil`
letfilename:String?}extensionNetworkRequest{
/// Returns HTTP body data formatted for a multipart/form-data request.
/// - Parameters:
/// - boundary: The boundary to use between items and at the end of the body.
/// - items: The items to list in the body.
/// - Returns: HTTP body data formatted for a multipart/form-data request.
func multipartFormDataHTTPBody(boundary:String, for items:[NetworkFormData])->Data?{vardata=Data()
for item in items {letfilenameString= item.filename.map{"; filename=\"\($0)\""}??""
data.append("--\(boundary)\r\n")
data.append("Content-Disposition: form-data; name=\"\(item.key)\"\(filenameString)\r\n")
data.append("Content-Type: \(item.mimeType)\r\n")
data.append("\r\n")
data.append(item.data)
data.append("\r\n")}
data.append("--\(boundary)--")return data
}}privateextensionData{mutatingfunc append(_ string:String){
guard let data = string.data(using:.utf8)else{assertionFailure("String could not be converted to UTF-8 data.")return}append(data)}}
The text was updated successfully, but these errors were encountered:
Engineering Task
Add a convenience for multi-part form data in an HTTP body, the bane of many iOS developers' existence when integrating with APIs. Here's an example of how it could look:
The text was updated successfully, but these errors were encountered: