Skip to main content

Knowledgebase

Search knowledgebase

Hints & Tips
How to determine resources sizes before downloading them

When using an agent to download a very large file, it is helpful to determine the size of the file first so that you can request the file in manageable chunks.

The standard way to do this is to use a HEAD request: call http.request() with "HEAD" as the first argument. The request can then be sent, and the response’s Content-Length header examined. Unfortunately, this operation may fail: the request will timeout.

To avoid this, make a GET request which contains the Range: bytes=0-0 header. The response will contain a Content-Range header which contains the size of the file: eg. 0-1/17616, where 17616 is the size of the file in bytes.

local resp = http.get("https://example.com/images/imp.png", {"Range": "bytes=0-0"}).sendsync();
local parts = split(resp.headers["content-range"], "/");
local filesize = parts[1].tointeger();