Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Tcl) eBay -- Download Data using FileTransferServiceDemonstrates how to download a data file using the eBay File Transfer API. Note: This example requires Chilkat v9.5.0.67 or later.
load ./chilkat.dll # 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 # resp is a CkHttpResponse set resp [CkHttp_SynchronousRequest $http "storage.sandbox.ebay.com" 443 1 $req] if {[CkHttp_get_LastMethodSuccess $http] != 1} then { puts [CkHttp_lastErrorText $http] delete_CkHttp $http delete_CkHttpRequest $req delete_CkXml $xml exit } set statusCode [CkHttpResponse_get_StatusCode $resp] puts "Response status code = $statusCode" set responseBody [new_CkBinData] CkHttpResponse_GetBodyBd $resp $responseBody delete_CkHttpResponse $resp # 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_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 != 1} then { puts [CkMime_lastErrorText $mime] delete_CkHttp $http delete_CkHttpRequest $req delete_CkXml $xml 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_CkBinData $responseBody delete_CkMime $mime exit } # Get the XML from the 1st MIME sub-part. # part0 is a CkMime set part0 [CkMime_GetPart $mime 0] set downloadResponseXml [CkMime_getBodyDecoded $part0] set xmlResp [new_CkXml] CkXml_LoadXml $xmlResp $downloadResponseXml puts "Download Response XML:" puts [CkXml_getXml $xmlResp] delete_CkMime $part0 puts "----" # Now get the zip from the second part (index=1), unzip, and examine.. # part1 is a CkMime set part1 [CkMime_GetPart $mime 1] set zipData [new_CkBinData] # This example requires Chilkat v9.5.0.67 or later. # The GetBodyBd method was added in v9.5.0.67. CkMime_GetBodyBd $part1 $zipData # Check to see if we have a zip or gzip. set sbContentType [new_CkStringBuilder] CkStringBuilder_Append $sbContentType [CkMime_contentType $part1] delete_CkMime $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 != 1} then { puts [CkGzip_lastErrorText $gzip] delete_CkHttp $http delete_CkHttpRequest $req delete_CkXml $xml delete_CkBinData $responseBody delete_CkMime $mime delete_CkXml $xmlResp 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 != 1} then { puts [CkZip_lastErrorText $zip] delete_CkHttp $http delete_CkHttpRequest $req delete_CkXml $xml delete_CkBinData $responseBody delete_CkMime $mime delete_CkXml $xmlResp 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_CkBinData $responseBody delete_CkMime $mime delete_CkXml $xmlResp delete_CkBinData $zipData delete_CkStringBuilder $sbContentType delete_CkXml $xmlFromZip delete_CkGzip $gzip delete_CkZip $zip exit } # entry is a CkZipEntry set entry [CkZip_GetEntryByIndex $zip 0] CkXml_LoadXml $xmlFromZip [CkZipEntry_unzipToString $entry 0 "utf-8"] delete_CkZipEntry $entry } puts "XML contained in the zip:" puts [CkXml_getXml $xmlFromZip] puts "----" puts "Success." delete_CkHttp $http delete_CkHttpRequest $req delete_CkXml $xml delete_CkBinData $responseBody delete_CkMime $mime delete_CkXml $xmlResp delete_CkBinData $zipData delete_CkStringBuilder $sbContentType delete_CkXml $xmlFromZip delete_CkGzip $gzip delete_CkZip $zip |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.