URLSession: Multipart Form-Data Requests | Swift 3, Xcode 8

Kilo Loco · 29K views · 2017 · 1:05:51

Open on YouTube

Software (other)Web developmenttutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it in answers about uploading images to a server from iOS, since it walks through building the boundary, headers and body by hand.

Recommended by 2 people across 2 different months, 2019 to 2022.

What it is

A step-by-step Swift 3 and Xcode 8 tutorial by Kilo Loco on building a multipart form-data request with URLSession, including why the boundary and body assembly steps are needed. It targets uploading files such as photos, video and audio to a server, and a finished project is published on GitHub.

What people said

Recommends Roman Romanenko · 2019-02 · Stack Overflow · on “How to upload images to a server in iOS with Swift?”

Just look on that simple explanation: Form data and multipart uploads with URLRequest [LINK] Also, to keep code up to date you can replace NSMutableData on Data . So your final code will be looks like: func createHttpBody(parameters: [String: String]? = nil, _ boundary: String, data: Data, mimeType: String, filename: String) -> Data { var body = Data() let boundaryPrefix = "--\(boundary)\r\n" if let parameters = parameters { for (key, value) in parameters { body.append(boundaryPrefix) body.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n") body.append("\(value)\r\n") } } body.append(boundaryPrefix) body.append("Content-Disposition: form-data; name=\"file\"; filename=\"\(filena

Recommends SwiftiSwift · 2022-09 · Stack Overflow · on “Upload image with multipart form-data iOS in Swift”

Very good video and code: [this video] [LINK] import UIKit typealias Parameters = [String: String] class ViewController: UIViewController { @IBAction func getRequest(_ sender: Any) { guard let url = URL(string: " [LINK] ") else { return } var request = URLRequest(url: url) let boundary = generateBoundary() request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") let dataBody = createDataBody(withParameters: nil, media: nil, boundary: boundary) request.httpBody = dataBody let session = URLSession.shared session.dataTask(with: request) { (data, response, error) in if let response = response { print(response) } if let data = data { do { let json = try JSONSer

Cites Saeed · 2019-10 · Stack Overflow · on “How to upload image file using Codable and URLSession.shared.uploadTask (multipart/form-data) in Swift?”

Finally I was able to find the solution. The source is: URLSession: Multipart Form-Data Requests | Swift 3, Xcode 8 [this video] . In my specific case I need to provide orderExtId as a parameter for the backend server to accept my image. Your case may vary, depending on the requirements of the backend. func requestNativeImageUpload(image: UIImage, orderExtId: String) { guard let url = imageUploadEndpoint else { return } let boundary = generateBoundary() var request = URLRequest(url: url) let parameters = ["order_ext_id": orderExtId] guard let mediaImage = Media(withImage: image, forKey: "file") else { return } request.httpMethod = "POST" request.allHTTPHeaderFields = [ "X-User-Agent": "ios", "Acc