Lua
Lua
QuickGetBd Example
The QuickGetBd method is called to send a GET request to download a binary target, such as PDF, zip, image file, etc. into a Chilkat BinData object.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
http.keep_response_body = true
local bd = chilkat.BinData.new()
-- Download the contents of a PDF file into the bd object.
local success = http:quick_get_bd("https://www.chilkatsoft.com/hello.pdf", bd)
if not success then
-- This will happen if the response status code was 400 or greater,
-- or if the request could not be sent, or if no response was received.
--
-- In other words, success will only be true if bd contains the desired content at the URL (not an error response).
local statusCode = http.last_status
print("Response status: " .. statusCode)
if statusCode == 0 then
-- There was an error in communications.
print(http.last_error_text)
else
-- We received a response status code indicating failure.
-- Examine the response body.
print(http.last_response_body)
end
return
end
-- Load the downloaded PDF into a Chilkat PDF object.
local pdf = chilkat.Pdf.new()
success = pdf:load_bd(bd)
if not success then
print(pdf.last_error_text)
return
end
-- ...
print("Success.")