Sample code for 30+ languages & platforms
Lua

Explaining the Rest ClearResponseBodyStream Method

See more Azure Cloud Storage Examples

The ClearResponseBodyStream method would be needed if your applicaiton called SetResponseBodyStream to send the response to a stream for one request, but then wants to subsequently do something else. The application must call ClearResponseBodyStream to no longer send responses to the stream.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local rest = chilkat.Rest.new()

-- Connect to the web server
local bTls = true
local port = 443
local bAutoReconnect = true
local success = rest:connect("www.chilkatsoft.com", port, bTls, bAutoReconnect)
if not success then
    print(rest.last_error_text)
    return
end

-- Setup a file stream for the download
local fileStream = chilkat.Stream.new()
fileStream.sink_file = "qa_output/starfish.jpg"

-- Indicate that the call to FullRequestNoBody should send the response body
-- to fileStream if the response status code is 200.
-- If a non-success response status code is received, then nothing
-- is streamed to the output file and the error response is returned by FullRequestNoBody.
local expectedStatus = 200
rest:set_response_body_stream(expectedStatus, true, fileStream)

local responseStr = rest:full_request_no_body("GET", "/images/starfish.jpg")
if not rest.last_method_success then
    -- Examine the request/response to see what happened.
    print("response status code = " .. rest.response_status_code)
    print("response status text = " .. rest.response_status_text)
    print("response header: " .. rest.response_header)
    print("response body (if any): " .. responseStr)
    print("---")
    print("LastRequestStartLine: " .. rest.last_request_start_line)
    print("LastRequestHeader: " .. rest.last_request_header)
    return
end

print("downloaded starfish.jpg")

-- Clear the response body stream previously set in the call to SetResponseBodyStream.
rest:clear_response_body_stream()

-- Download something else, but this time send the response body to bd.
local bd = chilkat.BinData.new()
success = rest:full_request_no_body_bd("GET", "/images/penguins.jpg", bd)
if not success then
    -- Examine the request/response to see what happened.
    print("response status code = " .. rest.response_status_code)
    print("response status text = " .. rest.response_status_text)
    print("response header: " .. rest.response_header)
    print("response body (if any): " .. bd:get_string("utf-8"))
    print("---")
    print("LastRequestStartLine: " .. rest.last_request_start_line)
    print("LastRequestHeader: " .. rest.last_request_header)
    return
end

-- Save the contents of bd to a file.
success = bd:write_file("qa_output/penguins.jpg")

print("Success: " .. tostring(success))