I'm using Alamofire 4 with swift 3
to update user profile. and also I'm using Router
class. What I need is to uplaod
and image with other parameters. I can update
users detail, without uploading the image part.
this is what it looks like in postman
so is it possible create an urlconvertible request for this . how can I upload the image with other parametes. (this works fine in postman). how can I do this with new Alamofire
. I tried it like below.
let parameters = [
"profile_image": "swift_file.jpeg"
]
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(UIImageJPEGRepresentation(profileImage, 1)!, withName: "file", fileName: "swift_file.jpeg", mimeType: "image/png")
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}, to:urltoUpdate)
{ (result) in
switch result {
case .success(let upload, _, _):
print("the status code is :")
upload.uploadProgress(closure: { (progress) in
print("something")
})
upload.responseJSON { response in
print("the resopnse code is : \(response.response?.statusCode)")
print("the response is : \(response)")
}
break
case .failure(let encodingError):
print("the error is : \(encodingError.localizedDescription)")
break
}
}
but this didn't work properly. hope your help with this part.
You do not need this:
"profile_image": "swift_file.jpeg"
And Parameters should be:
let parameters = [
"firstname": "Bill",
"surname": "fox",
//...rest of the parameters
]
And this withName: "file"
:
multipartFormData.append(UIImageJPEGRepresentation(profileImage, 1)!, withName: "file", fileName: "swift_file.jpeg", mimeType: "image/png")
Should be withName: "profile_image"
:
multipartFormData.append(UIImageJPEGRepresentation(profileImage, 1)!, withName: "profile_image", fileName: "swift_file.jpeg", mimeType: "image/png")
Code with Headers:
let parameters = [
"firstname": "Bill",
"surname": "fox",
//...rest of the parameters
]
let headers = [
"somekey": "somevalue",
//...rest of the parameters
]
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(UIImageJPEGRepresentation(profileImage, 1)!, withName: "profile_image", fileName: "swift_file.jpeg", mimeType: "image/png")
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}, usingThreshold:UInt64.init(),
to: "", //URL Here
method: .post,
headers: headers, //pass header dictionary here
encodingCompletion: { (result) in
switch result {
case .success(let upload, _, _):
print("the status code is :")
upload.uploadProgress(closure: { (progress) in
print("something")
})
upload.responseJSON { response in
print("the resopnse code is : \(response.response?.statusCode)")
print("the response is : \(response)")
}
break
case .failure(let encodingError):
print("the error is : \(encodingError.localizedDescription)")
break
}
})