1

totalBytesExpectedToRead returns -1 even Content-length has a value

I found the same problem here on stackoverflow but was not answered

Here's my code:

Alamofire.download(.POST, urlString, headers: headers, destination: destination)   
       .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
            print("totalBytesRead: \(totalBytesRead)")
            print("totalBytesExpectedToRead: \(totalBytesExpectedToRead)\n\n")
        }
        .response { request, response, data, error in
        }

Server response:

Access-Control-Allow-Origin →*
Access-Control-Expose-Headers →Link
Connection →keep-alive
Content-Length →163
Content-Type →application/json; charset=utf-8
Date →Tue, 04 Oct 2016 07:12:56 GMT

Result:

totalBytesRead: 1211
totalBytesExpectedToRead: -1
Community
  • 1
  • 1
acmel067
  • 73
  • 4

2 Answers2

1

There is definitely something wrong with what the server sends you: the announced Content-Length (163 bytes) is smaller that what you actually receive (1211 bytes).

It seems that there is only one call to the progress block, and I would say that Alamofire sets totalBytesExpectedToRead to -1 as a response to the incoherence between what is announced by the server and what is received.

François Legras
  • 308
  • 2
  • 12
0

Pass Accept-Encoding key in header.

It worked for me.

let headers = ["Accept-Encoding" : ""]

Alamofire.download(.POST, urlString, headers: headers, destination: destination)   
       .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
            print("totalBytesRead: \(totalBytesRead)")
            print("totalBytesExpectedToRead: \(totalBytesExpectedToRead)\n\n")
        }
        .response { request, response, data, error in
        }

Check accepted answer

Ravi
  • 93
  • 11