I need to upload an image with multipart/form-data i have tried :
static func uploadFile(image: UIImage?, parameters:Parameter,onResponse:@escaping(_ result:Parameter)-> (),onFailure:@escaping(_ error:Error)->()){
let url = "ip address"
let imageData = image?.jpegData(compressionQuality: 0.7)
let headers: HTTPHeaders = [
"Authorization": TOKEN!
]
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append("\(parameters.toJSON())".data(using: String.Encoding.utf8)!, withName: "entity")
if let data = imageData{
multipartFormData.append(data, withName: "mp3")
}
}, usingThreshold: UInt64.init(), to: url, method: .post, headers: getHeaders()) { (result) in
switch result{
case .success(let upload, _, _):
upload.responseJSON { response in
print("Succesfully uploaded",response.error)
if let err = response.error{
return
}
}
case .failure(let error):
print("Error in upload: \(error.localizedDescription)")
}
}
}
I need to send an entity model and the Image. but I have to send the image as a formData, not in body.
note : sending image with a "file" key and class with "entity" key
i can not solve it in swift side ....
errors :
Succesfully uploaded Optional(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.})))
read comments first.
do not forget to add fileName even if you set your fileName one step before file uploading.
try to use ToJsonString() instead of using ToJson()