func download(from url: URL, fileName: String, method: HTTPMethod, headers: HTTPHeaders?, parameters: Parameters?, to destination: FileManager.SearchPathDirectory, completion: @escaping (_ error: Error?, _ destinationURL: URL?) -> ()) {
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsURL = FileManager.default.urls(for: destination, in: .userDomainMask)[0]
let fileURL = documentsURL.appendingPathComponent(fileName)
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
Alamofire.download(url, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: headers, to: destination).response { response in
completion(response.error, response.destinationURL)
}
}
Запрос:
guard let url = URL(string: "http://*****.com/service/download") else { return }
let parameters: Parameters = [
"side": "RIGHT" ]
let headers: HTTPHeaders = [
"Content-Type": "application/json",
"Authorization": "Bearer token"
]
download(from: url, fileName: "MyFile.zip", method: .post, headers: headers, parameters: parameters, to: .documentDirectory) { error, url in
completion(error, url)
}
Почему это не работает? Должен ли я использовать responseam от Alamofire? Как? 'Или' Что?
Правильно ли то, что я делаю?