0

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!!

1
  • Personally I couldn't get the URL query parameters to work with MultipartFormData, so I'm not too sure. The way I solved it in my application was to build the URL with the query params myself, and then only append the file to the MultipartFormData and upload to that URL. Example: multipartFormData.append(file, withName: "file") and then the to: parameter on the upload method is to: "\(url)?email=\(email)&type=\(type)" but replace the query parameters with the ones you need. Commented May 8, 2017 at 15:50

1 Answer 1

1

I had the same issue and this is how I solved it

 var parameters = [ [ "name": "tdd_form", "value": "test" ], [ "name": "id", "value": "\(self.inventoryDeviceVal.recId!)" ] . . . etc ] Alamofire.upload( multipartFormData: { multipartFormData in //convert parameters to data for param in parameters { multipartFormData.append((param["value"]?.data(using: .utf8)!)!, withName: param["name"]!) } //Add the data //Mine was image , this is how I did it //let uuid = UUID().uuidString.lowercased() //multipartFormData.append(imgData, withName: "images[]", fileName: "\(uuid).jpg", mimeType: "image/jpeg") 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) } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Musa, You were absolutely right with how you set up your parameters. I had to massage some of those parameters for my own purpose, but it set me down the right path. Been working on this for weeks. I will post my changes so everyone can see.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.