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):
}
}
)
Well, since Multipart Form Data is intended to be used for binary ( and to for text) data transmission, I believe it's bad practice to send data in encoded to String over it.
Another disadvantage is impossibility to send more complex parameters like JSON.
That said, a better option would be to send all data in binary form, that is as Data.
Say I need to send this data
...alongside with user's picture:
For that I would convert that text data to JSON and then to binary alongside with image:
And then, finally send it via Multipart Form Data request:
I found the solution :) finally.
We can append data in the request as multipartformdata.
Below is my code.
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