I am trying to hook up image uploading in my application. I am using Alamofire and the multipartFormData
functionality within Alamofire to achieve this.
I have my function written to upload this image along with other attributes.
func savePreferences(parameters: [String:Any], image: UIImage, completion: @escaping (Response?, Error?) -> Void) {
let url = "http://localhost:3000/users/3b4e124d-3b3c-4c71-8e05-013e461c2892"
let imgData = image.jpegData(compressionQuality: 0.5)!
AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "profile_imgname",fileName: "ProfilePic_\(UserDefaults.standard.string(forKey: "user_id") ?? "").jpg", mimeType: "image/jpg")
for (key, value) in parameters {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}, to: url, method: .patch) { (result) in
switch result {
case .success(let upload):
upload.responseJSON { response in
if let err = response.error {
failure(err)
return
}
completion(response.result.value)
}
case .failure(let error):
print("Error in upload: \(error.localizedDescription)")
}
}
}
In the switch
statement at the end, in the case .success
and case .failure
, I am receiving the following error:
Type 'URLRequest' has no member 'failure'
Type 'URLRequest' has no member 'success'
I have used existing Stack Overflow resources to understand how this multipartFormData
works, and it seems that they follow this format to an extent.
Please see below:
Send POST parameters with MultipartFormData using Alamofire, in iOS Swift
How to upload pictures with Alamofire (post)?
Why am I getting the errors with URLRequest has no member failure
and how can I correct this?
CodePudding user response:
The Alamofire 5.0 migration document says this:
MultipartFormData
’s API has changed and the top level upload methods to create and upload MultipartFormData have been updated to match other request APIs, so it’s not longer necessary to deal with the Result of the multipart encoding.
(https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire 5.0 Migration Guide.md)
The code that you're using looks to be from an earlier version. In 5.0, the trailing closure uses a URLRequest
as its parameter (explaining the errors that you're seeing).
The Usage guide (https://github.com/Alamofire/Alamofire/blob/master/Documentation/Usage.md) provides this as an example of uploading multipart form data:
AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(Data("one".utf8), withName: "one")
multipartFormData.append(Data("two".utf8), withName: "two")
}, to: "https://httpbin.org/post")
.responseDecodable(of: HTTPBinResponse.self) { response in
debugPrint(response)
}
(HTTPBinResponse
is an example of a Decodable
that you can replace with your own).
Besides responseDecodable
, it looks like you can also use responseJSON
, responseData
, responseString
etc.
Note that all of these are appended with a .
after the closing )
of the call -- not as a trailing closure.