Sample code for 30+ languages & platforms
Lua

Send a REST Request with a Binary Body

See more REST Examples

Demonstrates Rest.FullRequestBd, which sends a complete request whose binary body is read from a BinData and stores the text response body in a StringBuilder.

The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background. FullRequestBd is used when the request payload is binary, such as an image or other file, while the response is textual (for example a JSON acknowledgement).

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local rest = chilkat.Rest.new()
local bTls = true
local bAutoReconnect = true
local success = rest:connect("example.com", 443, bTls, bAutoReconnect)
if not success then
    print(rest.last_error_text)
    return
end

-- The file paths are relative to the application's current working directory.  Absolute paths
-- may also be used.  Supply the paths appropriate to your own environment.

-- FullRequestBd sends a request whose binary body is read from a BinData and stores the text
-- response body in a StringBuilder.
local bdRequest = chilkat.BinData.new()
local bLoaded = bdRequest:load_file("qa_data/image.png")
if not bLoaded then
    print("Failed to load the request body file.")
    return
end

rest:add_header("Content-Type", "image/png")

local sbResponse = chilkat.StringBuilder.new()
success = rest:full_request_bd("PUT", "/api/images/1", bdRequest, sbResponse)
if not success then
    print(rest.last_error_text)
    return
end

local responseText = sbResponse:get_as_string()
if not sbResponse.last_method_success then
    print(sbResponse.last_error_text)
    return
end

print(responseText)