Sample code for 30+ languages & platforms
Lua

Example: Http.DownloadAppend method

Demonstrates the DownloadAppend method.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local targetPath = "c:/temp/qa_output/download.txt"

local http = chilkat.Http.new()
http.keep_response_body = true

-- Assume the target file in the local filesystem does not yet exist.
local success = http:download_append("https://chilkatsoft.com/testData/helloWorld.txt", targetPath)
local statusCode = http.last_status
if statusCode == 0 then
    -- Unable to either send the request or get the response.
    print(http.last_error_text)
    return
end

-- Should be 200.
print("Response status code: " .. statusCode)

-- Examine the contents of the file.
local sb = chilkat.StringBuilder.new()
sb:load_file(targetPath, "utf-8")
print(sb:get_as_string())

-- Output:
-- Response status code: 200
-- Hello World!

-- Download another text file and append to the target file.
success = http:download_append("https://chilkatsoft.com/testData/this_is_a_test.txt", targetPath)
statusCode = http.last_status
if statusCode == 0 then
    -- Unable to either send the request or get the response.
    print(http.last_error_text)
    return
end

-- Should be 200.
print("Response status code: " .. statusCode)

-- Examine the contents of the file.
sb:load_file(targetPath, "utf-8")
print(sb:get_as_string())

-- Output:
-- Response status code: 200
-- Hello World!This is a Test.

-- Delete the local target file.
local fac = chilkat.FileAccess.new()
fac:file_delete(targetPath)

-- Try to download a file that does not exist:
success = http:download_append("https://chilkatsoft.com/testData/does_not_exist.txt", targetPath)
statusCode = http.last_status
if statusCode == 0 then
    -- Unable to either send the request or get the response.
    print(http.last_error_text)
else
    -- We got a response, and we already know it wasn't a 200 success response.
    -- It should be a 404 not found.
    print("Response status code: " .. statusCode)
    -- Examine the response body.
    print("Response body:")
    print(http.last_response_body)
end