Lua Requires Chilkat v11.0.0+
Lua
eBay -- Download Data using FileTransferService
See more eBay Examples
Demonstrates how to download a data file using the eBay File Transfer API.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.
-- Use a previously obtained access token. The token should look something like this:
-- "AgAAAA**AQA ..."
local accessToken = "EBAY_ACCESS_TOKEN"
local http = chilkat.Http.new()
local req = chilkat.HttpRequest.new()
req.http_verb = "POST"
req.path = "/FileTransferService"
req.content_type = "application/xml"
-- Build the XML body for the request.
local xml = chilkat.Xml.new()
xml.tag = "downloadFileRequest"
xml:add_attribute("xmlns", "http://www.ebay.com/marketplace/services")
xml:update_child_content("taskReferenceId", "50013004806")
xml:update_child_content("fileReferenceId", "50015579016")
req:load_body_from_string(xml:get_xml(), "utf-8")
-- The XML body looks like this:
-- <?xml version="1.0" encoding="UTF-8"?>
-- <downloadFileRequest xmlns="http://www.ebay.com/marketplace/services">
-- <taskReferenceId>50013004806</taskReferenceId>
-- <fileReferenceId>50015579016</fileReferenceId>
-- </downloadFileRequest>
req:add_header("X-EBAY-SOA-OPERATION-NAME", "downloadFile")
req:add_header("X-EBAY-SOA-SECURITY-TOKEN", accessToken)
local resp = chilkat.HttpResponse.new()
local success = http:http_sreq("storage.sandbox.ebay.com", 443, true, req, resp)
if not success then
print(http.last_error_text)
return
end
local statusCode = resp.status_code
print("Response status code = " .. statusCode)
local responseBody = chilkat.BinData.new()
resp:get_body_bd(responseBody)
-- We can save the response body to a file for examination if we get an unanticipated response.
-- (It's binary, so it won't open well in a text editor.)
responseBody:write_file("qa_output/response.mime")
if statusCode ~= 200 then
print("Failed.")
return
end
-- The response body looks like this:
-- --MIMEBoundaryurn_uuid_2B668636C1E17A4D4114925305818684241
-- Content-Type: application/xop+xml; charset=utf-8; type="text/xml"
-- Content-Transfer-Encoding: binary
-- Content-ID: <0.urn:uuid:2B668636C1E17A4D4114925305818684242>
--
-- <?xml version='1.0' encoding='UTF-8'?>
-- <downloadFileResponse xmlns="http://www.ebay.com/marketplace/services">
-- <ack>Success</ack>
-- <version>1.1.0</version>
-- <timestamp>2017-04-18T15:49:41.868Z</timestamp>
-- <fileAttachment>
-- <Size>587</Size>
-- <Data>
-- <xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:urn:uuid:A37C3C73E994C267F11492530585522"/>
-- </Data>
-- </fileAttachment>
-- </downloadFileResponse>
-- --MIMEBoundaryurn_uuid_2B668636C1E17A4D4114925305818684241
-- Content-Type: application/zip
-- Content-Transfer-Encoding: binary
-- Content-ID: <urn:uuid:A37C3C73E994C267F11492530585522>
--
-- <the binary bytes of the zip start here...>
--
-- Load the binary response into a MIME object.
local mime = chilkat.Mime.new()
success = mime:load_mime_bd(responseBody)
if not success then
print(mime.last_error_text)
return
end
-- Make sure we have 2 sub-parts. The 1st sub-part is the XML response, the 2nd sub-part
-- is the zip containing the data.
-- Note: The 2nd sub-part can be a "zip" or "gzip". These are two different file formats.
-- A zip is indicated with a Content-Type header equal to "application/zip",
-- whereas a gzip is indicated with a Content-Type header equal to "application/x-gzip"
if mime.num_parts ~= 2 then
print("Expected the MIME to have 2 parts.")
print("NumParts = " .. mime.num_parts)
print("Failed.")
return
end
-- Get the XML from the 1st MIME sub-part.
local part0 = chilkat.Mime.new()
success = mime:part_at(0, part0)
if not success then
print(mime.last_error_text)
return
end
local downloadResponseXml = part0:get_body_decoded()
local xmlResp = chilkat.Xml.new()
xmlResp:load_xml(downloadResponseXml)
print("Download Response XML:")
print(xmlResp:get_xml())
print("----")
-- Now get the zip from the second part (index=1), unzip, and examine..
local part1 = chilkat.Mime.new()
success = mime:part_at(1, part1)
if not success then
print(mime.last_error_text)
return
end
local zipData = chilkat.BinData.new()
part1:get_body_bd(zipData)
-- Check to see if we have a zip or gzip.
local sbContentType = chilkat.StringBuilder.new()
sbContentType:append(part1.content_type)
local xmlFromZip = chilkat.Xml.new()
if sbContentType:contains("gzip", false) then
-- This is a gzip compressed file.
local gzip = chilkat.Gzip.new()
-- in-place uncompress the data.
success = gzip:uncompress_bd(zipData)
if not success then
print(gzip.last_error_text)
return
end
xmlFromZip:load_xml(zipData:get_string("utf-8"))
else
-- This is a zip archive.
-- Load the body into a Zip object.
local zip = chilkat.Zip.new()
success = zip:open_bd(zipData)
if not success then
print(zip.last_error_text)
return
end
-- Save the .zip to a file (so we can examine it for debugging if something is not as expected)
zipData:write_file("qa_output/ebay_data.zip")
-- The zip should contain a single XML file.
if zip.num_entries ~= 1 then
print("Expected the .zip to have 1 entry.")
print("NumEntries = " .. zip.num_entries)
print("Failed.")
return
end
local entry = chilkat.ZipEntry.new()
success = zip:entry_at(0, entry)
if not success then
print(zip.last_error_text)
return
end
xmlFromZip:load_xml(entry:unzip_to_string(0, "utf-8"))
end
print("XML contained in the zip:")
print(xmlFromZip:get_xml())
print("----")
print("Success.")