Lua
Lua
QR Code Generator via api.qrserver.com REST API
See more HTTP Examples
Demonstrates how to generate a QR code using the api.qrserver.com REST API service.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- Send the following GET request to get a binary response.
-- The body of the binary response contains the image data for the QR code.
-- https://api.qrserver.com/v1/create-qr-code/?data=[URL-encoded-text]&size=[pixels]x[pixels]
local queryParams = chilkat.JsonObject.new()
queryParams:update_string("data", "Hello World")
queryParams:update_string("size", "100x100")
-- Get a PNG file..
-- Possible formats are:
-- png
-- gif
-- jpeg
-- jpg
-- svg
-- eps
-- Case matters. Use lowercase.
queryParams:update_string("format", "png")
-- Send the GET request to an endpoint.
-- Chilkat will add the url-encoded query params passed in the JSON.
local resp = chilkat.HttpResponse.new()
local success = http:http_params("GET", "https://api.qrserver.com/v1/create-qr-code/", queryParams, resp)
if not success then
print(http.last_error_text)
return
end
-- Did it succeed?
if resp.status_code ~= 200 then
-- The response body, if anything, would not be the image data, but would likely be the error text (or HTML, or whatever...)
print(resp.body_str)
print("Response status = " .. resp.status_code)
print("Failed.")
return
end
-- Success if we get here..
-- Save the binary body as the PNG file, or you can get the bytes of the PNG..
success = resp:save_body_binary("c:/temp/qa_output/qr_code.png")
-- Or get the bytes:
local bd = chilkat.BinData.new()
resp:get_body_bd(bd)
-- Use the bytes in bd...
-- See the online reference documentation for the function to access the bytes directly.
print("Success.")