Sample code for 30+ languages & platforms
PowerBuilder

eBay -- Download Data using FileTransferService

See more eBay Examples

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

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
string ls_AccessToken
oleobject loo_Http
oleobject loo_Req
oleobject loo_Xml
oleobject loo_Resp
integer li_StatusCode
oleobject loo_ResponseBody
oleobject loo_Mime
oleobject loo_Part0
string ls_DownloadResponseXml
oleobject loo_XmlResp
oleobject loo_Part1
oleobject loo_ZipData
oleobject loo_SbContentType
oleobject loo_XmlFromZip
oleobject loo_Gzip
oleobject loo_Zip
oleobject loo_Entry

li_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 ..."
ls_AccessToken = "EBAY_ACCESS_TOKEN"

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Req = create oleobject
li_rc = loo_Req.ConnectToNewObject("Chilkat.HttpRequest")

loo_Req.HttpVerb = "POST"
loo_Req.Path = "/FileTransferService"
loo_Req.ContentType = "application/xml"

// Build the XML body for the request.
loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")

loo_Xml.Tag = "downloadFileRequest"
loo_Xml.AddAttribute("xmlns","http://www.ebay.com/marketplace/services")
loo_Xml.UpdateChildContent("taskReferenceId","50013004806")
loo_Xml.UpdateChildContent("fileReferenceId","50015579016")

loo_Req.LoadBodyFromString(loo_Xml.GetXml(),"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>

loo_Req.AddHeader("X-EBAY-SOA-OPERATION-NAME","downloadFile")
loo_Req.AddHeader("X-EBAY-SOA-SECURITY-TOKEN",ls_AccessToken)

loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpSReq("storage.sandbox.ebay.com",443,1,loo_Req,loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Req
    destroy loo_Xml
    destroy loo_Resp
    return
end if

li_StatusCode = loo_Resp.StatusCode
Write-Debug "Response status code = " + string(li_StatusCode)

loo_ResponseBody = create oleobject
li_rc = loo_ResponseBody.ConnectToNewObject("Chilkat.BinData")

loo_Resp.GetBodyBd(loo_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.)
loo_ResponseBody.WriteFile("qa_output/response.mime")

if li_StatusCode <> 200 then
    Write-Debug "Failed."
    destroy loo_Http
    destroy loo_Req
    destroy loo_Xml
    destroy loo_Resp
    destroy loo_ResponseBody
    return
end if

// 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.
loo_Mime = create oleobject
li_rc = loo_Mime.ConnectToNewObject("Chilkat.Mime")

li_Success = loo_Mime.LoadMimeBd(loo_ResponseBody)
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Http
    destroy loo_Req
    destroy loo_Xml
    destroy loo_Resp
    destroy loo_ResponseBody
    destroy loo_Mime
    return
end if

// 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 loo_Mime.NumParts <> 2 then
    Write-Debug "Expected the MIME to have 2 parts."
    Write-Debug "NumParts = " + string(loo_Mime.NumParts)
    Write-Debug "Failed."
    destroy loo_Http
    destroy loo_Req
    destroy loo_Xml
    destroy loo_Resp
    destroy loo_ResponseBody
    destroy loo_Mime
    return
end if

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

loo_Part0 = create oleobject
li_rc = loo_Part0.ConnectToNewObject("Chilkat.Mime")

li_Success = loo_Mime.PartAt(0,loo_Part0)
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Http
    destroy loo_Req
    destroy loo_Xml
    destroy loo_Resp
    destroy loo_ResponseBody
    destroy loo_Mime
    destroy loo_Part0
    return
end if

ls_DownloadResponseXml = loo_Part0.GetBodyDecoded()
loo_XmlResp = create oleobject
li_rc = loo_XmlResp.ConnectToNewObject("Chilkat.Xml")

loo_XmlResp.LoadXml(ls_DownloadResponseXml)
Write-Debug "Download Response XML:"
Write-Debug loo_XmlResp.GetXml()

Write-Debug "----"

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

loo_Part1 = create oleobject
li_rc = loo_Part1.ConnectToNewObject("Chilkat.Mime")

li_Success = loo_Mime.PartAt(1,loo_Part1)
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Http
    destroy loo_Req
    destroy loo_Xml
    destroy loo_Resp
    destroy loo_ResponseBody
    destroy loo_Mime
    destroy loo_Part0
    destroy loo_XmlResp
    destroy loo_Part1
    return
end if

loo_ZipData = create oleobject
li_rc = loo_ZipData.ConnectToNewObject("Chilkat.BinData")

loo_Part1.GetBodyBd(loo_ZipData)

// Check to see if we have a zip or gzip.
loo_SbContentType = create oleobject
li_rc = loo_SbContentType.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbContentType.Append(loo_Part1.ContentType)

loo_XmlFromZip = create oleobject
li_rc = loo_XmlFromZip.ConnectToNewObject("Chilkat.Xml")

if loo_SbContentType.Contains("gzip",0) = 1 then
    // This is a gzip compressed file.
    loo_Gzip = create oleobject
    li_rc = loo_Gzip.ConnectToNewObject("Chilkat.Gzip")

    // in-place uncompress the data.
    // Note: The UncompressBd method was added in Chilkat v9.5.0.67
    li_Success = loo_Gzip.UncompressBd(loo_ZipData)
    if li_Success = 0 then
        Write-Debug loo_Gzip.LastErrorText
        destroy loo_Http
        destroy loo_Req
        destroy loo_Xml
        destroy loo_Resp
        destroy loo_ResponseBody
        destroy loo_Mime
        destroy loo_Part0
        destroy loo_XmlResp
        destroy loo_Part1
        destroy loo_ZipData
        destroy loo_SbContentType
        destroy loo_XmlFromZip
        destroy loo_Gzip
        return
    end if

    loo_XmlFromZip.LoadXml(loo_ZipData.GetString("utf-8"))

else
    // This is a zip archive.

    // Load the body into a Zip object.
    loo_Zip = create oleobject
    li_rc = loo_Zip.ConnectToNewObject("Chilkat.Zip")

    li_Success = loo_Zip.OpenBd(loo_ZipData)
    if li_Success = 0 then
        Write-Debug loo_Zip.LastErrorText
        destroy loo_Http
        destroy loo_Req
        destroy loo_Xml
        destroy loo_Resp
        destroy loo_ResponseBody
        destroy loo_Mime
        destroy loo_Part0
        destroy loo_XmlResp
        destroy loo_Part1
        destroy loo_ZipData
        destroy loo_SbContentType
        destroy loo_XmlFromZip
        destroy loo_Gzip
        destroy loo_Zip
        return
    end if

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

    // The zip should contain a single XML file.
    if loo_Zip.NumEntries <> 1 then
        Write-Debug "Expected the .zip to have 1 entry."
        Write-Debug "NumEntries = " + string(loo_Zip.NumEntries)
        Write-Debug "Failed."
        destroy loo_Http
        destroy loo_Req
        destroy loo_Xml
        destroy loo_Resp
        destroy loo_ResponseBody
        destroy loo_Mime
        destroy loo_Part0
        destroy loo_XmlResp
        destroy loo_Part1
        destroy loo_ZipData
        destroy loo_SbContentType
        destroy loo_XmlFromZip
        destroy loo_Gzip
        destroy loo_Zip
        return
    end if

    loo_Entry = create oleobject
    li_rc = loo_Entry.ConnectToNewObject("Chilkat.ZipEntry")

    li_Success = loo_Zip.EntryAt(0,loo_Entry)
    if li_Success = 0 then
        Write-Debug loo_Zip.LastErrorText
        destroy loo_Http
        destroy loo_Req
        destroy loo_Xml
        destroy loo_Resp
        destroy loo_ResponseBody
        destroy loo_Mime
        destroy loo_Part0
        destroy loo_XmlResp
        destroy loo_Part1
        destroy loo_ZipData
        destroy loo_SbContentType
        destroy loo_XmlFromZip
        destroy loo_Gzip
        destroy loo_Zip
        destroy loo_Entry
        return
    end if

    loo_XmlFromZip.LoadXml(loo_Entry.UnzipToString(0,"utf-8"))
end if

Write-Debug "XML contained in the zip:"
Write-Debug loo_XmlFromZip.GetXml()
Write-Debug "----"

Write-Debug "Success."


destroy loo_Http
destroy loo_Req
destroy loo_Xml
destroy loo_Resp
destroy loo_ResponseBody
destroy loo_Mime
destroy loo_Part0
destroy loo_XmlResp
destroy loo_Part1
destroy loo_ZipData
destroy loo_SbContentType
destroy loo_XmlFromZip
destroy loo_Gzip
destroy loo_Zip
destroy loo_Entry