I'm trying to upload a photo on the server and I have to set the request headers and parameters.
In postman the request looks like image below:
I don't know how to set first key "scan". I tried to set for the scan key the image local path, the image size but without result.
I tried in this way to upload the image on server :
Alamofire.upload(multipartFormData: { (multipartFormData) in
for (key, value) in params {
multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
}
if let data = imageData{
multipartFormData.append(data, withName: "image", fileName: "image.png", mimeType: "image/png")
}
}, usingThreshold: UInt64.init(), to: ApiClientURLs.photo, method: .post, headers: headers) { (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (Progress) in
print("Upload Progress: \(Progress.fractionCompleted)")
})
upload.responseJSON { response in
//self.delegate?.showSuccessAlert()
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
// self.showSuccesAlert()
//self.removeImage("frame", fileExtension: "txt")
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
case .failure(let encodingError):
//self.delegate?.showFailAlert()
print(encodingError)
}
}
Colleagues from the server side told me that it's a problem with the multipart upload and sent me this error:
default message [scan]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile' for property 'scan'
The headers and parameters request looks like:
let headers: HTTPHeaders = [
"Content-type" : "multipart/form-data",
"X-Email" : decodedLoginModel.email ?? "",
"Token" : decodedLoginModel.jwt ?? ""]
var params: [String: Any] = [
//"scan" : imagePath
"contact_email": contact_email_switch,
"contact_mail": contact_mail_switch,
"contact_phone": contact_phone_switch,
"contact_sms": contact_sms_switch,
"required_id": Int.random(in:1111...1999) ]
Can you help me to fix the upload ?