Lua
Lua
REST File Upload (multipart/form-data)
See more REST Examples
Demonstrates how to upload a file using multipart/form-data.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 rest = chilkat.Rest.new()
-- Connect to the HTTP server using TLS.
local bTls = true
local port = 443
local bAutoReconnect = true
-- Make sure to replace "www.chilkatsoft.com" with your domain..
local success = rest:connect("www.chilkatsoft.com", port, bTls, bAutoReconnect)
if not success then
print(rest.last_error_text)
return
end
local fileStream = chilkat.Stream.new()
fileStream.source_file = "qa_data/jpg/starfish.jpg"
rest:add_header("Content-Type", "multipart/form-data")
rest.part_selector = "1"
rest:add_header("Content-Type", "image/jpg")
rest:add_header("Content-Disposition", "form-data; name=\"filename\"; filename=\"starfish.jpg\"")
rest:set_multipart_body_stream(fileStream)
rest.part_selector = "0"
local responseBody = rest:full_request_multipart("POST", "/the_uri_path_to_receive_the_upload")
if not rest.last_method_success then
print(rest.last_error_text)
return
end
if rest.response_status_code ~= 200 then
print("Received error response code: " .. rest.response_status_code)
return
end
print("File uploaded")