I'm trying to upload an array of pictures along with some parameters using Alamofire 4 and Swift 3.
The parameters seem to work because the update is done, but the images don't get to the server
In Postman I can do this fine with no problem:
This is my code so far:
let parameters = [
"service_request_id" : servicesID,
"status_id" : "4",
]
Alamofire.upload(
multipartFormData: { multipartFormData in
for (key,value) in parameters {
if let value = value as? String {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}
for (image) in self.imagesArray {
if let imageData = UIImageJPEGRepresentation(image, 1) {
multipartFormData.append(imageData, withName: "image", fileName: "image.jpeg", mimeType: "image/jpeg")
}
}
},
to: ConnectionWS.UpdateServicesURL,
method: .put,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print(progress)
})
upload.responseJSON { response in
// If the request to get activities is succesfull, store them
if response.result.isSuccess{
print(response.debugDescription)
alert.dismiss(animated: true, completion:
{
self.dismiss(animated: true, completion:
{
self.delegate?.statusChanged(IsFinish: false)
})
})
// Else throw an error
} else {
var errorMessage = "ERROR MESSAGE: "
if let data = response.data {
// Print message
let responseJSON = JSON(data: data)
if let message: String = responseJSON["error"]["message"].string {
if !message.isEmpty {
errorMessage += message
}
}
}
print(errorMessage) //Contains General error message or specific.
print(response.debugDescription)
}
alert.dismiss(animated: true, completion:
{
self.dismiss(animated: true, completion:nil)
})
}
case .failure(let encodingError):
print("FALLE ------------")
print(encodingError)
}
}
)
Am I doing something wrong?
Please help.