I want to know why can't I give a file as one of the parameter values and to send the parameter as an upload request in Alamofire. I tried like this and it works.
let parameters: [String : AnyObject ] = [
"email" : "abc@gmail.com",
"password" : "password",
"full_name": "XXX"]
let image: UIImage? = UIImage(named: "logo.png")
Alamofire.upload(
.POST,
"http://myurl.com/register",
multipartFormData: { multipartFormData in
for (key, value) in parameters {
multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
}
if let imageData = UIImageJPEGRepresentation(image!, 1) {
multipartFormData.appendBodyPart(data: imageData, name: "profile_pic", fileName: "logo.png", mimeType: "image/png")
}
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .Failure(let encodingError):
print(encodingError)
}
}
)
Why can't I pass the image as a parameter value? And also why should I need to change the image in UIImageJPEGRepresentation
.
If I pass the image as a value without changing the representation, error throws that it is not in NSData
format.
Note that the request should pass even if there is an image or not, because it is an optional one.