Sample code for 30+ languages & platforms
Lua

REST Download Binary File to Memory

See more REST Examples

Download a binary file to a Chilkat BinData object.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local rest = chilkat.Rest.new()

-- We're going to download a sample MS-Word doc file.
-- The URLs of our MS-Word sample documents are:

-- https://www.chilkatdownload.com/sample_data/sample.doc
-- https://www.chilkatdownload.com/sample_data/sample.docx

local pathPartOfUrl = "/sample_data/sample.doc"
local domain = "chilkatdownload.com"

local bTls = true
local port = 443
local bAutoReconnect = true
local success = rest:connect(domain, port, bTls, bAutoReconnect)
if not success then
    print(rest.last_error_text)
    return
end

local bd = chilkat.BinData.new()
success = rest:full_request_no_body_bd("GET", pathPartOfUrl, bd)
if not success then
    print(rest.last_error_text)
    return
end

-- A 200 response is expected for actual success.
-- If we don't get a 200 response, then the response body was not actually
-- the file data, but it was text containing error information.
if rest.response_status_code ~= 200 then
    local sbErrorText = chilkat.StringBuilder.new()
    sbErrorText:append_bd(bd, "utf-8", 0, 0)
    print(sbErrorText:get_as_string())
    print("-- Failed.")
    return
end

-- Save to a local file.
-- Change the file path based on your operating system or needs...
success = bd:write_file("c:/temp/qa_output/sample.doc")
if not success then
    print("Failed to save to local file.")
    return
end

print("REST Download of MS-Word File was successful.")