Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

eBay -- Download Data using FileTransferService

See more eBay Examples

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

Chilkat Rust Downloads

Rust

// 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 ..."
let access_token = "EBAY_ACCESS_TOKEN".to_string();

let http = chilkat::Http::new();

let req = chilkat::HttpRequest::new();

req.set_http_verb("POST");
req.set_path("/FileTransferService");
req.set_content_type("application/xml");

// Build the XML body for the request.
let xml = chilkat::Xml::new();
xml.set_tag("downloadFileRequest");
let _ = xml.add_attribute("xmlns", "http://www.ebay.com/marketplace/services");
xml.update_child_content("taskReferenceId", "50013004806");
xml.update_child_content("fileReferenceId", "50015579016");

let _ = req.load_body_from_string(&xml.get_xml().unwrap_or_default(), "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", &access_token);

let resp = chilkat::HttpResponse::new();
if http.http_s_req("storage.sandbox.ebay.com", 443, true, &req, &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

let status_code = resp.status_code();
println!("Response status code = {}", status_code);

let response_body = chilkat::BinData::new();
let _ = resp.get_body_bd(&response_body);

// 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.)
let _ = response_body.write_file("qa_output/response.mime");

if status_code != 200 {
    println!("Failed.");
    return;
}

// 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.
let mime = chilkat::Mime::new();
if mime.load_mime_bd(&response_body).is_err() {
    println!("{}", mime.last_error_text());
    return;
}

// 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 {
    println!("Expected the MIME to have 2 parts.");
    println!("NumParts = {}", mime.num_parts());
    println!("Failed.");
    return;
}

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

let part0 = chilkat::Mime::new();
if mime.part_at(0, &part0).is_err() {
    println!("{}", mime.last_error_text());
    return;
}

let download_response_xml = part0.get_body_decoded().unwrap_or_default();
let xml_resp = chilkat::Xml::new();
let _ = xml_resp.load_xml(&download_response_xml);
println!("Download Response XML:");
println!("{}", xml_resp.get_xml().unwrap_or_default());

println!("----");

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

let part1 = chilkat::Mime::new();
if mime.part_at(1, &part1).is_err() {
    println!("{}", mime.last_error_text());
    return;
}

let zip_data = chilkat::BinData::new();
let _ = part1.get_body_bd(&zip_data);

// Check to see if we have a zip or gzip.
let sb_content_type = chilkat::StringBuilder::new();
let _ = sb_content_type.append(&part1.content_type());

let xml_from_zip = chilkat::Xml::new();

if sb_content_type.contains("gzip", false) {
    // This is a gzip compressed file.
    let gzip = chilkat::Gzip::new();

    // in-place uncompress the data.
    // Note: The UncompressBd method was added in Chilkat v9.5.0.67
    if gzip.uncompress_bd(&zip_data).is_err() {
        println!("{}", gzip.last_error_text());
        return;
    }

    let _ = xml_from_zip.load_xml(&zip_data.get_string("utf-8").unwrap_or_default());

} else {
    // This is a zip archive.

    // Load the body into a Zip object.
    let zip = chilkat::Zip::new();
    if zip.open_bd(&zip_data).is_err() {
        println!("{}", zip.last_error_text());
        return;
    }

    // Save the .zip to a file (so we can examine it for debugging if something is not as expected)
    let _ = zip_data.write_file("qa_output/ebay_data.zip");

    // The zip should contain a single XML file.
    if zip.num_entries() != 1 {
        println!("Expected the .zip to have 1 entry.");
        println!("NumEntries = {}", zip.num_entries());
        println!("Failed.");
        return;
    }

    let entry = chilkat::ZipEntry::new();
    if zip.entry_at(0, &entry).is_err() {
        println!("{}", zip.last_error_text());
        return;
    }

    let _ = xml_from_zip.load_xml(&entry.unzip_to_string(0, "utf-8").unwrap_or_default());
}

println!("XML contained in the zip:");
println!("{}", xml_from_zip.get_xml().unwrap_or_default());
println!("----");

println!("Success.");