Sample code for 30+ languages & platforms
DataFlex

eBay -- Download Data using FileTransferService

See more eBay Examples

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

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    String sAccessToken
    Handle hoHttp
    Variant vReq
    Handle hoReq
    Handle hoXml
    Variant vResp
    Handle hoResp
    Integer iStatusCode
    Variant vResp
onseBody    Handle hoResponseBody
    Handle hoMime
    Variant vPart0
    Handle hoPart0
    String sDownloadResponseXml
Resp    Handle hoXmlResp
    Variant vPart1
    Handle hoPart1
    Variant vZipData
    Handle hoZipData
    Handle hoSbContentType
    Handle hoXmlFromZip
    Handle hoGzip
    Handle hoZip
    Variant vEntry
    Handle hoEntry
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    // 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 ..."
    Move "EBAY_ACCESS_TOKEN" To sAccessToken

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    Get Create (RefClass(cComChilkatHttpRequest)) To hoReq
    If (Not(IsComObjectCreated(hoReq))) Begin
        Send CreateComObject of hoReq
    End

    Set ComHttpVerb Of hoReq To "POST"
    Set ComPath Of hoReq To "/FileTransferService"
    Set ComContentType Of hoReq To "application/xml"

    // Build the XML body for the request.
    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Set ComTag Of hoXml To "downloadFileRequest"
    Get ComAddAttribute Of hoXml "xmlns" "http://www.ebay.com/marketplace/services" To iSuccess
    Send ComUpdateChildContent To hoXml "taskReferenceId" "50013004806"
    Send ComUpdateChildContent To hoXml "fileReferenceId" "50015579016"

    Get ComGetXml Of hoXml To sTemp1
    Get ComLoadBodyFromString Of hoReq sTemp1 "utf-8" To iSuccess

    // 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>

    Send ComAddHeader To hoReq "X-EBAY-SOA-OPERATION-NAME" "downloadFile"
    Send ComAddHeader To hoReq "X-EBAY-SOA-SECURITY-TOKEN" sAccessToken

    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get pvComObject of hoReq to vReq
    Get pvComObject of hoResp to vResp
    Get ComHttpSReq Of hoHttp "storage.sandbox.ebay.com" 443 True vReq vResp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iStatusCode
    Showln "Response status code = " iStatusCode

    Get Create (RefClass(cComChilkatBinData)) To hoResponseBody
    If (Not(IsComObjectCreated(hoResponseBody))) Begin
        Send CreateComObject of hoResponseBody
    End
    Get pvComObject of hoResponseBody to vResponseBody
    Get ComGetBodyBd Of hoResp vResponseBody To iSuccess

    // 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.)
    Get ComWriteFile Of hoResponseBody "qa_output/response.mime" To iSuccess

    If (iStatusCode <> 200) Begin
        Showln "Failed."
        Procedure_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.
    Get Create (RefClass(cComChilkatMime)) To hoMime
    If (Not(IsComObjectCreated(hoMime))) Begin
        Send CreateComObject of hoMime
    End
    Get pvComObject of hoResponseBody to vResponseBody
    Get ComLoadMimeBd Of hoMime vResponseBody To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_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"
    Get ComNumParts Of hoMime To iTemp1
    If (iTemp1 <> 2) Begin
        Showln "Expected the MIME to have 2 parts."
        Get ComNumParts Of hoMime To iTemp1
        Showln "NumParts = " iTemp1
        Showln "Failed."
        Procedure_Return
    End

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

    Get Create (RefClass(cComChilkatMime)) To hoPart0
    If (Not(IsComObjectCreated(hoPart0))) Begin
        Send CreateComObject of hoPart0
    End
    Get pvComObject of hoPart0 to vPart0
    Get ComPartAt Of hoMime 0 vPart0 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComGetBodyDecoded Of hoPart0 To sDownloadResponseXml
    Get Create (RefClass(cComChilkatXml)) To hoXmlResp
    If (Not(IsComObjectCreated(hoXmlResp))) Begin
        Send CreateComObject of hoXmlResp
    End
    Get ComLoadXml Of hoXmlResp sDownloadResponseXml To iSuccess
    Showln "Download Response XML:"
    Get ComGetXml Of hoXmlResp To sTemp1
    Showln sTemp1

    Showln "----"

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

    Get Create (RefClass(cComChilkatMime)) To hoPart1
    If (Not(IsComObjectCreated(hoPart1))) Begin
        Send CreateComObject of hoPart1
    End
    Get pvComObject of hoPart1 to vPart1
    Get ComPartAt Of hoMime 1 vPart1 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatBinData)) To hoZipData
    If (Not(IsComObjectCreated(hoZipData))) Begin
        Send CreateComObject of hoZipData
    End
    Get pvComObject of hoZipData to vZipData
    Get ComGetBodyBd Of hoPart1 vZipData To iSuccess

    // Check to see if we have a zip or gzip.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbContentType
    If (Not(IsComObjectCreated(hoSbContentType))) Begin
        Send CreateComObject of hoSbContentType
    End
    Get ComContentType Of hoPart1 To sTemp1
    Get ComAppend Of hoSbContentType sTemp1 To iSuccess

    Get Create (RefClass(cComChilkatXml)) To hoXmlFromZip
    If (Not(IsComObjectCreated(hoXmlFromZip))) Begin
        Send CreateComObject of hoXmlFromZip
    End

    Get ComContains Of hoSbContentType "gzip" False To bTemp1
    If (bTemp1 = True) Begin
        // This is a gzip compressed file.
        Get Create (RefClass(cComChilkatGzip)) To hoGzip
        If (Not(IsComObjectCreated(hoGzip))) Begin
            Send CreateComObject of hoGzip
        End

        // in-place uncompress the data.
        // Note: The UncompressBd method was added in Chilkat v9.5.0.67
        Get pvComObject of hoZipData to vZipData
        Get ComUncompressBd Of hoGzip vZipData To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoGzip To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Get ComGetString Of hoZipData "utf-8" To sTemp1
        Get ComLoadXml Of hoXmlFromZip sTemp1 To iSuccess

    End
    Else Begin
        // This is a zip archive.

        // Load the body into a Zip object.
        Get Create (RefClass(cComChilkatZip)) To hoZip
        If (Not(IsComObjectCreated(hoZip))) Begin
            Send CreateComObject of hoZip
        End
        Get pvComObject of hoZipData to vZipData
        Get ComOpenBd Of hoZip vZipData To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoZip To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        // Save the .zip to a file (so we can examine it for debugging if something is not as expected)
        Get ComWriteFile Of hoZipData "qa_output/ebay_data.zip" To iSuccess

        // The zip should contain a single XML file.
        Get ComNumEntries Of hoZip To iTemp1
        If (iTemp1 <> 1) Begin
            Showln "Expected the .zip to have 1 entry."
            Get ComNumEntries Of hoZip To iTemp1
            Showln "NumEntries = " iTemp1
            Showln "Failed."
            Procedure_Return
        End

        Get Create (RefClass(cComChilkatZipEntry)) To hoEntry
        If (Not(IsComObjectCreated(hoEntry))) Begin
            Send CreateComObject of hoEntry
        End
        Get pvComObject of hoEntry to vEntry
        Get ComEntryAt Of hoZip 0 vEntry To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoZip To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Get ComUnzipToString Of hoEntry 0 "utf-8" To sTemp1
        Get ComLoadXml Of hoXmlFromZip sTemp1 To iSuccess
    End

    Showln "XML contained in the zip:"
    Get ComGetXml Of hoXmlFromZip To sTemp1
    Showln sTemp1
    Showln "----"

    Showln "Success."


End_Procedure