Search code examples
swiftdownloadalamofireresponsemessage

How to get error message from response body when use Alamofire.download() request


AF.download(url, parameters: params, to: destination)
  .validate(statusCode: [200])
  .response { response in
    switch response.result {
    case .success(let url):
      print("ok", url)
    case .failure(let err):
      print(err.localizedDescription)
    }
  }

When server response some error code like 4xx, 5xx, it will print "Response status code was unacceptable: xxx.", but I want the detail message in response body(server send plain text when error), I read some post that said we can retrieve response message with "response.data" but if use AF.download method, there isn't have "data" property with response object(Alamofire.AFDownloadResponse). so, is there any way to figure it out?


Solution

  • Alamofire's DownloadResponse contains a fileURL: URL? property which can be used to load the downloaded data from disk even when validation or other actions produce a failure result in the response.