I am trying to upload data through alamofire including images. But it returns
responseSerializationFailed(Alamofire.AFError.ResponseSerializationFai
Try this for uploading image with parameter with alamofire
Alamofire.upload(multipartFormData: { (multipartFormData) in
let imageData: NSData? = UIImageJPEGRepresentation(image, 0.5) as NSData?
if imageData != nil {
multipartFormData.append(imageData! as Data, withName: strImageKey, fileName: "imagep.jpeg", mimeType: "imagep/jpeg")
for (key, value) in dictParams {
let data = "\(value)".data(using: .utf8)
multipartFormData.append(data! as Data, withName: key)
}
}
}, with: requestName,encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
log.debug("Image(s) Uploaded successfully:\(response)")
//Networking.handleApiResponse(response)
}
case .failure(let encodingError):
log.debug("encodingError:\(encodingError)")
// Networking.handleApiResponse(response)
Util.showAlertWithMessage(msgSorry, title:"Error")
}
completionHandler!(encodingResult)
}
)
Just change from responseJSON
to responseString
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append((userid?.data(using: String.Encoding.utf8))!, withName: "user_id")
multipartFormData.append((self.addUserBtnTag?.description.data(using: String.Encoding.utf8))!, withName: "slot")
}, to: urlString, method: .post, encodingCompletion: { (result) in
switch result {
case .success(let upload, _, _):
upload.responseString { response in
print(response.result)
print("Response : ", response)
if response.result.isSuccess
{
let JSON = response.result.value as? NSDictionary
print("EditJSON : ", JSON!)
}
}
case .failure(let encodingError):
print(encodingError)
}
})