I have upload the image with some parameters with the multipart method, I have getting success response in iPhoneX (12.3.1) and in iPhone7 iOS(12.3.1, 12.4) getting error
(Alamofire.AFError.responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})))
I have use Alamofire with below configuration
Alamofire version: 4.7.2
Xcode version: 10.2.1
Swift version:4.2
Platform(s) running Alamofire: iOS 12.3
macOS version running Xcode: 10.14.5
let url = "\(BASE_DOMAIN)/xxxx”
var headers: HTTPHeaders = [:]
if defaults.object(forKey: "authKey") == nil{
headers = [
"authKey":""
]
}else{
headers = [
"authKey":defaults.object(forKey: "authKey") as! String,
]
}
let sessionManager = Alamofire.SessionManager.default
sessionManager.session.configuration.timeoutIntervalForRequest = 300
sessionManager.upload(multipartFormData: { (multipartFormData) in
let reqDic:NSDictionary = self.perpareRequestData(isCommisionCharge: false)
print("product total key \(reqDic.allKeys)")
let productName:String = reqDic["product_name"] as? String ?? ""
multipartFormData.append(productName.data(using: String.Encoding.utf8)!, withName: "prodctname")
multipartFormData.append("382".data(using: String.Encoding.utf8)!, withName: "productId")
let productImage:NSArray = reqDic["product_img"] as? NSArray ?? []
var productImageCount:Int = -1
if productImage.count>0{
for valueData in productImage {
productImageCount = productImageCount+1
let dt:Data = valueData as!Data
let productName:String = reqDic["product_name"] as? String ?? ""
let imageName:String = productName.trimmingCharacters(in: .whitespaces) + String(self.toMillis())
multipartFormData.append(dt as Data, withName: "productImgData[\(productImageCount)]", fileName: imageName + ".jpg", mimeType: "image/*")
}
}else{
let imageName:String = productName.trimmingCharacters(in: .whitespaces) + String(self.toMillis())
multipartFormData.append("".data(using: String.Encoding.utf8)!, withName: "productImgData[\(productImageCount)]", fileName: imageName + ".jpg", mimeType: "image/*")
}
}, usingThreshold: 0, to: url, method: HTTPMethod.post, headers: headers) { (result) in
self.view.isUserInteractionEnabled = true
//10000000000
print(result)
switch result {
case .success(let upload, _, _):
upload.responseJSON { response in
print(response.result.description) // result of response description
print(response.result.error.debugDescription) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
let dic:NSDictionary = JSON as! NSDictionary
let isSuccess:Bool = dic["success"] as? Bool ?? false
if isSuccess{
GlobalData.sharedInstance.showSuccessSnackBar(msg: dic["message"] as?String ?? "Your request completed successfully" )
}else {
GlobalData.sharedInstance.showErrorSnackBar(msg: dic["message"] as?String ?? "Something went wrong" )
}
}
}
case .failure(let encodingError):
GlobalData.sharedInstance.dismissLoader()
GlobalData.sharedInstance.showErrorSnackBar(msg:"Something went wrong" )
print(encodingError.localizedDescription)
}
}
}