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):
}
}
)
This is how i solve my problem
For Swift 4.2 / Alamofire 4.7.3
Also you could take a look at CodyFire lib it makes API calls easier using Codable for everything. Example for Multipart call using CodyFire
You could take a look at all the examples in lib's readme
In Alamofire 4 it is important to add the body data before you add the file data!
for alamofire 4 use this ..
Swift 3 / Alamofire 4.0 (Addendum to the accepted answer)
To append to
multipartFormData
in Swift 3 / Alamofire 4.0, use the following method ofMultipartFormData
:And, to convert
String
toData
, thedata(using:)
method ofString
. E.g.,As in Swift 3.x for upload image with parameter we can use below alamofire upload method-