Skip to content

Instantly share code, notes, and snippets.

@Tayphoon
Created March 4, 2019 13:13
Show Gist options
  • Save Tayphoon/ec4554d9ce60431e49e8cb4853c75b75 to your computer and use it in GitHub Desktop.
Save Tayphoon/ec4554d9ce60431e49e8cb4853c75b75 to your computer and use it in GitHub Desktop.
import Foundation
import Alamofire
extension MultipartFormData {
/// Creates a body part from the data and appends it to the multipart form data object.
///
/// The body part data will be encoded using the following format:
///
/// - `Content-Disposition: form-data; name=#{name}` (HTTP Header)
/// - `Content-Type: #{generated mimeType}` (HTTP Header)
/// - Encoded data
/// - Multipart form boundary
///
/// - parameter object: The data to encode into the multipart form data.
/// - parameter name: The name to associate with the data in the `Content-Disposition` HTTP header.
/// - parameter decoder: The encoder to use to encode the `object` parameter. Defaults to a `JSONEncoder`
/// - parameter mimeType: The MIME type to associate with the data content type in the `Content-Type` HTTP header.
public func append<T: Encodable>(_ object: T, withName name: String, encoder: JSONEncoder = JSONEncoder(), mimeType: String = "application/json") throws {
let data = try encoder.encode(object)
append(data, withName: name, mimeType: mimeType)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment