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

eBay -- Upload Bulk Data using FileTransferService

See more eBay Examples

Demonstrates how to upload your data file using the eBay File Transfer API.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // 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 ..."
    const access_token = "EBAY_ACCESS_TOKEN";

    const http = try chilkat.Http.init();
    defer http.deinit();

    const api_call = "uploadFile";
    const file_attachment_uuid = "<urn:uuid:bb47b86a237311e793ae92361f002671>";
    const xml_uuid = "<urn:uuid:bb47b766237311e793ae92361f002671>";

    const req = try chilkat.HttpRequest.init();
    defer req.deinit();

    req.setHttpVerb("POST");
    req.setPath("/FileTransferService");

    const sb_content_type = try chilkat.StringBuilder.init();
    defer sb_content_type.deinit();
    sb_content_type.append("multipart/related; type=\"application/xop+xml\"; start=\"XMLUUID\"; start-info=\"text/xml\"") catch {};
    _ = sb_content_type.replace("XMLUUID", xml_uuid);
    req.setContentType(try sb_content_type.getAsString(alloc));

    req.addHeader("X-EBAY-SOA-SERVICE-NAME", "FileTransferService");
    req.addHeader("X-EBAY-SOA-OPERATION-NAME", api_call);
    req.addHeader("X-EBAY-SOA-SECURITY-TOKEN", access_token);
    req.addHeader("X-EBAY-SOA-REQUEST-DATA-FORMAT", "XML");
    req.addHeader("X-EBAY-SOA-RESPONSE-DATA-FORMAT", "XML");
    req.addHeader("User-Agent", "AnythingYouWant");

    const path_to_file_on_disk1 = "qa_data/ebay/uploadFileRequest.xml";
    req.addFileForUpload("uploadFileRequest.xml", path_to_file_on_disk1) catch {
        std.debug.print("{s}\n", .{try req.getLastErrorText(alloc)});
        return;
    };

    const path_to_file_on_disk2 = "qa_data/ebay/BulkDataExchangeRequests.gz";
    req.addFileForUpload("BulkDataExchangeRequests.gz", path_to_file_on_disk2) catch {
        std.debug.print("{s}\n", .{try req.getLastErrorText(alloc)});
        return;
    };

    // Add sub-headers for each file in the request.
    req.addSubHeader(0, "Content-Type", "application/xop+xml; charset=UTF-8; type=\"text/xml\"") catch {};
    req.addSubHeader(0, "Content-Transfer-Encoding", "binary") catch {};
    req.addSubHeader(0, "Content-ID", xml_uuid) catch {};
    req.addSubHeader(1, "Content-Type", "application/octet-stream") catch {};
    req.addSubHeader(1, "Content-Transfer-Encoding", "binary") catch {};
    req.addSubHeader(1, "Content-ID", file_attachment_uuid) catch {};

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpSReq("storage.sandbox.ebay.com", 443, true, req, resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Response status code = {d}\n", .{resp.getStatusCode()});

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.loadXml(try resp.getBodyStr(alloc)) catch {};

    if (resp.getStatusCode() != 200) {
        std.debug.print("{s}\n", .{try xml.getXml(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // We still may have a failure.  The XML needs to be checked.
    // A failed response might look like this:

    //     <?xml version="1.0" encoding="UTF-8" ?>
    //     <uploadFileResponse xmlns="http://www.ebay.com/marketplace/services">
    //         <ack>Failure</ack>
    //         <errorMessage>
    //             <error>
    //                 <errorId>1</errorId>
    //                 <domain>Marketplace</domain>
    //                 <severity>Error</severity>
    //                 <category>Application</category>
    //                 <message>Task Reference Id is invalid</message>
    //                 <subdomain>FileTransfer</subdomain>
    //             </error>
    //         </errorMessage>
    //         <version>1.1.0</version>
    //         <timestamp>2017-04-18T01:05:27.475Z</timestamp>
    //     </uploadFileResponse>

    // A successful response looks like this:

    //     <?xml version="1.0" encoding="UTF-8" ?>
    //     <uploadFileResponse xmlns="http://www.ebay.com/marketplace/services">
    //         <ack>Success</ack>
    //         <version>1.1.0</version>
    //         <timestamp>2017-04-18T01:22:47.853Z</timestamp>
    //     </uploadFileResponse>

    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    // Get the "ack" to see if it's "Failure" or "Success"
    if (xml.childContentMatches("ack", "Success", false)) {
        std.debug.print("Success.\n", .{});
    } else {
        std.debug.print("Failure.\n", .{});
    }
}