I am having difficulty getting the right Alamofire syntax to upload a file. The server interface is a Rest API. I have a number of parameters I need to pass to hit the correct database and the specific record. Example:
"https://ram23jtog.tt.rp.com/ewws/EWUpdate/.json?$KB=ITIL&$table=asset_table&$login=testuser2&$password=pwd123&$lang=en&id=18005&tdd_form=2017-5-8-911-6633442.pdf" One thing to note on the parameters is the "ID" parameter needs to be toward the end so that the table and login information is read first - or so it would appear.
I have tried to load the parameters up then append them later:
let parameters: Parameters = [ "&tdd_form" : "test", "&id" : "\(self.inventoryDeviceVal.recId!)", "$lang" : "en", "$password" : "\(self.sharedDataVal.appUserPassword!)", "$login" : "\(self.sharedDataVal.appUserID!)", "$table" : "asset_table", "$KB" : "ITIL" ] for (key, value) in parameters { multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key) } It looks to me like the parameters are getting truncated or there is an error that is preventing some of them from loading.
Alamofire Request:
Alamofire.upload( multipartFormData: { multipartFormData in for (key, value) in parameters { multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key) } multipartFormData.append(fileURL, withName: fileName, fileName: fileName, mimeType: "application/pdf") }, to: updateCriteria) { (result) in switch result { case .success(let upload, _, _): upload.responseJSON { response in //self.delegate?.showSuccessAlert() print(response.request!) // original URL request print(response.response!) // URL response print(response.data!) // server data print(response.result) // result of response serialization // self.showSuccesAlert() if let JSON = response.result.value { print("JSON: \(JSON)") } } case .failure(let encodingError): //self.delegate?.showFailAlert() print(encodingError) } } } Any Suggestions? Would it be easier to do a simple Alamofire request instead of a multipart upload?
RESOLUTION
Reconfigured my parameters per Musa's suggestion:
let parameters = [ [ "name" : "$KB", "value" : "ITIL" ], [ "name" : "$table", "value" : "asset_table" ], [ "name" : "$login", "value" : "\(self.sharedDataVal.appUserID!)" ], [ "name" : "$password", "value" : "\(self.sharedDataVal.appUserPassword!)" ], [ "name" : "$lang", "value" : "en" ], [ "name" : "id", "value" : "\(self.inventoryDeviceVal.recId!)" ] ] I had the parameter "tdd_form" up in the parameters definition. I adjusted the append so that it is coded correctly with the "tdd_form" parameter in the right section now that the parameters were passing correctly.
Alamofire.upload( multipartFormData: { multipartFormData in for param in parameters { multipartFormData.append((param["value"]?.data(using: .utf8)!)!, withName: param["name"]!) } multipartFormData.append(fileURL, withName: "tdd_form", fileName: fileName, mimeType: "application/pdf") }, to: updateCriteria) { (result) in switch result { case .success(let upload, _, _): upload.responseJSON { response in //self.delegate?.showSuccessAlert() print(response.request!) // original URL request print(response.response!) // URL response print(response.data!) // server data print(response.result) // result of response serialization // self.showSuccesAlert() if let JSON = response.result.value { print("JSON: \(JSON)") } } case .failure(let encodingError): //self.delegate?.showFailAlert() print(encodingError) } } Works perfectly!!
multipartFormData.append(file, withName: "file")and then theto:parameter on theuploadmethod isto: "\(url)?email=\(email)&type=\(type)"but replace the query parameters with the ones you need.