Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send POST parameters with MultipartFormData using Alamofire, in iOS Swift

I am using Alamofire, very first time. I am using the latest version Alamofire 1.3.1. I want to send one image , one video and some POST parameters in one API call. I am using multipart form data. The mutipart module is working. I am facing a problem to send extra POST parametersparams . Below is my code. "params" is the dictionary which contains extra parameters? How can I append these POST parameters in the request. Please help

        var fullUrl :String = Constants.BASE_URL + "/api/CompleteChallenge"          var params = [         "authKey": Constants.AuthKey,         "idUserChallenge": "16",         "comment": "",         "photo": imagePath,         "video": videoPath,         "latitude": "1",         "longitude": "1",         "location": "india"     ]      let imagePathUrl = NSURL(fileURLWithPath: imagePath!)     let videoPathUrl = NSURL(fileURLWithPath: videoPath!)          Alamofire.upload(         .POST,         URLString: fullUrl, // http://httpbin.org/post         multipartFormData: { multipartFormData in             multipartFormData.appendBodyPart(fileURL: imagePathUrl!, name: "photo")             multipartFormData.appendBodyPart(fileURL: videoPathUrl!, name: "video")         },         encodingCompletion: { encodingResult in             switch encodingResult {             case .Success(let upload, _, _):                 upload.responseJSON { request, response, JSON, error in                    }                 }             case .Failure(let encodingError):              }         }     ) 
like image 251
Ankush Avatar asked Aug 11 '15 18:08

Ankush


People also ask

What is the use of Alamofire in Swift?

Alamofire is an HTTP networking library written in Swift. Alamofire helps to improve the quality of code. It is a simpler way to consume REST services. Alamofire is the basic tool for hundreds of projects.

What is Alamofire in iOS?

Alamofire is a networking library written in Swift. You use it to make HTTP(S) requests on iOS, macOS and other Apple platforms. For example, to post data to a web-based REST API or to download an image from a webserver. Alamofire has a convenient API built on top of URLSession (“URL Loading System”).


2 Answers

I found the solution :) finally.

We can append data in the request as multipartformdata.

Below is my code.

  Alamofire.upload(         .POST,         URLString: fullUrl, // http://httpbin.org/post         multipartFormData: { multipartFormData in             multipartFormData.appendBodyPart(fileURL: imagePathUrl!, name: "photo")             multipartFormData.appendBodyPart(fileURL: videoPathUrl!, name: "video")             multipartFormData.appendBodyPart(data: Constants.AuthKey.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"authKey")             multipartFormData.appendBodyPart(data: "\(16)".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"idUserChallenge")             multipartFormData.appendBodyPart(data: "comment".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"comment")             multipartFormData.appendBodyPart(data:"\(0.00)".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"latitude")             multipartFormData.appendBodyPart(data:"\(0.00)".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"longitude")             multipartFormData.appendBodyPart(data:"India".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"location")         },         encodingCompletion: { encodingResult in             switch encodingResult {             case .Success(let upload, _, _):                 upload.responseJSON { request, response, JSON, error in                   }             case .Failure(let encodingError):              }         }     ) 

EDIT 1: For those who are trying to send an array instead of float, int or string, They can convert their array or any kind of data-structure in Json String, pass this JSON string as a normal string. And parse this json string at backend to get original array

like image 190
Ankush Avatar answered Oct 05 '22 23:10

Ankush


In Alamofire 4 it is important to add the body data before you add the file data!

let parameters = [String: String]() [...] self.manager.upload(     multipartFormData: { multipartFormData in         for (key, value) in parameters {             multipartFormData.append(value.data(using: .utf8)!, withName: key)         }         multipartFormData.append(imageData, withName: "user", fileName: "user.jpg", mimeType: "image/jpeg")     },     to: path,     [...] ) 
like image 27
Alexander Scholz Avatar answered Oct 05 '22 23:10

Alexander Scholz