i want to upload photo with another parameters i use imagePickerController
internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imagePicked.image = info[UIImagePickerControllerOriginalImage] as? UIImage
dismiss(animated:true, completion: nil)
}
and i use this function to handle the service but can't work
public static func addCar(photo:UIImage, api_token: String, plate_number: String, type: Int, enddate: String, completion: @escaping (_ error: Error?,_ success: Bool)->Void){
let parameters = ["api_token": api_token,"plate_number":plate_number,"type": type, "enddate": enddate] as [String : Any]
let AddCarUrl = APIHelper.BASE_URL + APIHelper.API.ADD_CAR.rawValue
var jsonData : Data!
do {
jsonData = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
// here "jsonData" is the dictionary encoded in JSON data
let decoded = try JSONSerialization.jsonObject(with: jsonData, options: [])
// here "decoded" is of type `Any`, decoded from JSON data
// you can now cast it with the right type
if let dictFromJSON = decoded as? [String:String] {
// use dictFromJSON
print(dictFromJSON)
}
} catch {
print(error.localizedDescription)
}
Alamofire.upload(multipartFormData: { (form: MultipartFormData) in
form.append(UIImageJPEGRepresentation(photo, 0.5)!, withName: "photo", fileName: "photo", mimeType: "photo/jpeg")
form.append(jsonData, withName: "data")
print(jsonData)
}, usingThreshold: SessionManager.multipartFormDataEncodingMemoryThreshold,
to: AddCarUrl,
method: .post,
headers: nil) { (result: SessionManager.MultipartFormDataEncodingResult) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print(progress)
}).responseJSON(completionHandler: { (response) in
switch response.result {
case .success(let value):
let json = JSON(value)
print(json)
case .failure(let error):
print("Error: ",error)
}
})
case .failure(let encodingError):
print(encodingError)
}
}
}
and i get the error that some data is missing
<NSProgress: 0x60400012cd00> : Parent: 0x0 / Fraction completed: 0.0165 / Completed: 8192 of 497021
: Parent: 0x0 / Fraction completed: 0.4615 / Completed: 229376 of 497021
: Parent: 0x0 / Fraction completed: 0.6758 / Completed: 335872 of 497021
: Parent: 0x0 / Fraction completed: 0.9065 / Completed: 450560 of 497021
: Parent: 0x0 / Fraction completed: 1.0000 / Completed: 497021 of 497021
{
"code" : "404",
"errors" : {
"type" : [
"The type field is required."
],
"image" : [
"The image field is required."
],
"plate_number" : [
"The plate number field is required."
]
}
}
why i get this error although i upload image and plate_number and type.
can anyone help me.
thanks in advance