Search code examples
iosafnetworkingafnetworking-2

AFNetworking: download request by range of bytes (ie: only file header)


I have a working implementation of AFNetworking for downloading entire files (as per the documentation) over an https connection, however I was wondering if there is a way to download ONLY the start of a file so that I can parse some custom header elements stored at the start of the files myself.

Possibilities:

  1. Set a range of 0-1024 bytes and download that to a local file.
  2. As The streamed downloads seem to be read in 1024byte chunks it might be possible to cancel the download after 1 successful chunk with a callback?

If anyone has any ideas or can point me to documentation regarding this (I can't find any) let me know. Thank you!


Solution

  • I have found the solution! I was thinking about it incorrectly at first by trying to limit AFNetwork or using a callback to stop the download. The solution is in fact much easier...

    You need to tell the server that you only need "0 to n" number of bytes in the header of the GET request that you send to the server.

    [request.requestSerializer setValue:@"bytes=0-1024" forHTTPHeaderField:@"Range"];
    

    By adding this to the request, you will ask for only the bytes from 0 to 1024.

    Hope this helps someone!