0

i need to request the server with the url and request body but the response will be a zip file i need to download the zip file and use it so i used the following method but i need to know how to save the file and reuse it but getting the following error

Task <11E36FDA-1408-474D-B576-A222DEBA53B2>.<3> finished with error - code: -1005

     func performSyncRequest()
{
    let documentsUrl =  FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first
    let url = syncUrl.prepareSyncURL()
    var urlrequest = URLRequest(url: url)
    urlrequest.httpMethod = "POST"
    urlrequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
    let requestBody = syncUrl.prepareRequestBody()
    guard let httpBody = try? JSONSerialization.data(withJSONObject: requestBody, options: []) else { return }
    urlrequest.httpBody = httpBody
    do {
        let convertedString = String(data: httpBody, encoding: String.Encoding.utf8)
        print("Sync Request Body: \(convertedString!)")
    }

    let downloadTask = URLSession.shared.downloadTask(with: urlrequest, completionHandler: { url,response,error in
        if error == nil {
            //some code

            do {
                try FileManager.default.copyItem(at:url!, to: self.documentsUrl!)
                print(self.documentsUrl!)
            } catch (let writeError) {
                print("error writing file \(self.documentsUrl!) : \(writeError)")
            }

        }
    })

    downloadTask.resume()

}

1 Answers1

0

here the issue is while trying to download zip file their is no content length header so inorder to solve the issue i used the following code and it is working well

 urlrequest.addValue("", forHTTPHeaderField: "Accept-Encoding")