Sample code for 30+ languages & platforms
Tcl

eBay -- Download Data using FileTransferService

See more eBay Examples

Demonstrates how to download a data file using the eBay File Transfer API.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

# 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 ..."
set accessToken "EBAY_ACCESS_TOKEN"

set http [new_CkHttp]

set req [new_CkHttpRequest]

CkHttpRequest_put_HttpVerb $req "POST"
CkHttpRequest_put_Path $req "/FileTransferService"
CkHttpRequest_put_ContentType $req "application/xml"

# Build the XML body for the request.
set xml [new_CkXml]

CkXml_put_Tag $xml "downloadFileRequest"
CkXml_AddAttribute $xml "xmlns" "http://www.ebay.com/marketplace/services"
CkXml_UpdateChildContent $xml "taskReferenceId" "50013004806"
CkXml_UpdateChildContent $xml "fileReferenceId" "50015579016"

CkHttpRequest_LoadBodyFromString $req [CkXml_getXml $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>

CkHttpRequest_AddHeader $req "X-EBAY-SOA-OPERATION-NAME" "downloadFile"
CkHttpRequest_AddHeader $req "X-EBAY-SOA-SECURITY-TOKEN" $accessToken

set resp [new_CkHttpResponse]

set success [CkHttp_HttpSReq $http "storage.sandbox.ebay.com" 443 1 $req $resp]
if {$success == 0} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    delete_CkHttpRequest $req
    delete_CkXml $xml
    delete_CkHttpResponse $resp
    exit
}

set statusCode [CkHttpResponse_get_StatusCode $resp]
puts "Response status code = $statusCode"

set responseBody [new_CkBinData]

CkHttpResponse_GetBodyBd $resp $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.)
CkBinData_WriteFile $responseBody "qa_output/response.mime"

if {$statusCode != 200} then {
    puts "Failed."
    delete_CkHttp $http
    delete_CkHttpRequest $req
    delete_CkXml $xml
    delete_CkHttpResponse $resp
    delete_CkBinData $responseBody
    exit
}

# 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.
set mime [new_CkMime]

set success [CkMime_LoadMimeBd $mime $responseBody]
if {$success == 0} then {
    puts [CkMime_lastErrorText $mime]
    delete_CkHttp $http
    delete_CkHttpRequest $req
    delete_CkXml $xml
    delete_CkHttpResponse $resp
    delete_CkBinData $responseBody
    delete_CkMime $mime
    exit
}

# 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 {[CkMime_get_NumParts $mime] != 2} then {
    puts "Expected the MIME to have 2 parts."
    puts "NumParts = [CkMime_get_NumParts $mime]"
    puts "Failed."
    delete_CkHttp $http
    delete_CkHttpRequest $req
    delete_CkXml $xml
    delete_CkHttpResponse $resp
    delete_CkBinData $responseBody
    delete_CkMime $mime
    exit
}

# Get the XML from the 1st MIME sub-part.

set part0 [new_CkMime]

set success [CkMime_PartAt $mime 0 $part0]
if {$success == 0} then {
    puts [CkMime_lastErrorText $mime]
    delete_CkHttp $http
    delete_CkHttpRequest $req
    delete_CkXml $xml
    delete_CkHttpResponse $resp
    delete_CkBinData $responseBody
    delete_CkMime $mime
    delete_CkMime $part0
    exit
}

set downloadResponseXml [CkMime_getBodyDecoded $part0]
set xmlResp [new_CkXml]

CkXml_LoadXml $xmlResp $downloadResponseXml
puts "Download Response XML:"
puts [CkXml_getXml $xmlResp]

puts "----"

# Now get the zip from the second part (index=1), unzip, and examine..

set part1 [new_CkMime]

set success [CkMime_PartAt $mime 1 $part1]
if {$success == 0} then {
    puts [CkMime_lastErrorText $mime]
    delete_CkHttp $http
    delete_CkHttpRequest $req
    delete_CkXml $xml
    delete_CkHttpResponse $resp
    delete_CkBinData $responseBody
    delete_CkMime $mime
    delete_CkMime $part0
    delete_CkXml $xmlResp
    delete_CkMime $part1
    exit
}

set zipData [new_CkBinData]

CkMime_GetBodyBd $part1 $zipData

# Check to see if we have a zip or gzip.
set sbContentType [new_CkStringBuilder]

CkStringBuilder_Append $sbContentType [CkMime_contentType $part1]

set xmlFromZip [new_CkXml]

if {[CkStringBuilder_Contains $sbContentType "gzip" 0] == 1} then {
    # This is a gzip compressed file.
    set gzip [new_CkGzip]

    # in-place uncompress the data.
    # Note: The UncompressBd method was added in Chilkat v9.5.0.67
    set success [CkGzip_UncompressBd $gzip $zipData]
    if {$success == 0} then {
        puts [CkGzip_lastErrorText $gzip]
        delete_CkHttp $http
        delete_CkHttpRequest $req
        delete_CkXml $xml
        delete_CkHttpResponse $resp
        delete_CkBinData $responseBody
        delete_CkMime $mime
        delete_CkMime $part0
        delete_CkXml $xmlResp
        delete_CkMime $part1
        delete_CkBinData $zipData
        delete_CkStringBuilder $sbContentType
        delete_CkXml $xmlFromZip
        delete_CkGzip $gzip
        exit
    }

    CkXml_LoadXml $xmlFromZip [CkBinData_getString $zipData "utf-8"]

} else {
    # This is a zip archive.

    # Load the body into a Zip object.
    set zip [new_CkZip]

    set success [CkZip_OpenBd $zip $zipData]
    if {$success == 0} then {
        puts [CkZip_lastErrorText $zip]
        delete_CkHttp $http
        delete_CkHttpRequest $req
        delete_CkXml $xml
        delete_CkHttpResponse $resp
        delete_CkBinData $responseBody
        delete_CkMime $mime
        delete_CkMime $part0
        delete_CkXml $xmlResp
        delete_CkMime $part1
        delete_CkBinData $zipData
        delete_CkStringBuilder $sbContentType
        delete_CkXml $xmlFromZip
        delete_CkGzip $gzip
        delete_CkZip $zip
        exit
    }

    # Save the .zip to a file (so we can examine it for debugging if something is not as expected)
    CkBinData_WriteFile $zipData "qa_output/ebay_data.zip"

    # The zip should contain a single XML file.
    if {[CkZip_get_NumEntries $zip] != 1} then {
        puts "Expected the .zip to have 1 entry."
        puts "NumEntries = [CkZip_get_NumEntries $zip]"
        puts "Failed."
        delete_CkHttp $http
        delete_CkHttpRequest $req
        delete_CkXml $xml
        delete_CkHttpResponse $resp
        delete_CkBinData $responseBody
        delete_CkMime $mime
        delete_CkMime $part0
        delete_CkXml $xmlResp
        delete_CkMime $part1
        delete_CkBinData $zipData
        delete_CkStringBuilder $sbContentType
        delete_CkXml $xmlFromZip
        delete_CkGzip $gzip
        delete_CkZip $zip
        exit
    }

    set entry [new_CkZipEntry]

    set success [CkZip_EntryAt $zip 0 $entry]
    if {$success == 0} then {
        puts [CkZip_lastErrorText $zip]
        delete_CkHttp $http
        delete_CkHttpRequest $req
        delete_CkXml $xml
        delete_CkHttpResponse $resp
        delete_CkBinData $responseBody
        delete_CkMime $mime
        delete_CkMime $part0
        delete_CkXml $xmlResp
        delete_CkMime $part1
        delete_CkBinData $zipData
        delete_CkStringBuilder $sbContentType
        delete_CkXml $xmlFromZip
        delete_CkGzip $gzip
        delete_CkZip $zip
        delete_CkZipEntry $entry
        exit
    }

    CkXml_LoadXml $xmlFromZip [CkZipEntry_unzipToString $entry 0 "utf-8"]
}

puts "XML contained in the zip:"
puts [CkXml_getXml $xmlFromZip]
puts "----"

puts "Success."

delete_CkHttp $http
delete_CkHttpRequest $req
delete_CkXml $xml
delete_CkHttpResponse $resp
delete_CkBinData $responseBody
delete_CkMime $mime
delete_CkMime $part0
delete_CkXml $xmlResp
delete_CkMime $part1
delete_CkBinData $zipData
delete_CkStringBuilder $sbContentType
delete_CkXml $xmlFromZip
delete_CkGzip $gzip
delete_CkZip $zip
delete_CkZipEntry $entry