Sample code for 30+ languages & platforms
Unicode C

eBay -- Upload Bulk Data using FileTransferService

See more eBay Examples

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

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkHttpRequestW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkXmlW.h>

void ChilkatSample(void)
    {
    BOOL success;
    const wchar_t *accessToken;
    HCkHttpW http;
    const wchar_t *apiCall;
    const wchar_t *fileAttachmentUuid;
    const wchar_t *xmlUuid;
    HCkHttpRequestW req;
    HCkStringBuilderW sbContentType;
    int replaceCount;
    const wchar_t *pathToFileOnDisk1;
    const wchar_t *pathToFileOnDisk2;
    HCkHttpResponseW resp;
    HCkXmlW xml;

    success = FALSE;

    // 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 ..."
    accessToken = L"EBAY_ACCESS_TOKEN";

    http = CkHttpW_Create();

    apiCall = L"uploadFile";
    fileAttachmentUuid = L"<urn:uuid:bb47b86a237311e793ae92361f002671>";
    xmlUuid = L"<urn:uuid:bb47b766237311e793ae92361f002671>";

    req = CkHttpRequestW_Create();

    CkHttpRequestW_putHttpVerb(req,L"POST");
    CkHttpRequestW_putPath(req,L"/FileTransferService");

    sbContentType = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbContentType,L"multipart/related; type=\"application/xop+xml\"; start=\"XMLUUID\"; start-info=\"text/xml\"");
    replaceCount = CkStringBuilderW_Replace(sbContentType,L"XMLUUID",xmlUuid);
    CkHttpRequestW_putContentType(req,CkStringBuilderW_getAsString(sbContentType));

    CkHttpRequestW_AddHeader(req,L"X-EBAY-SOA-SERVICE-NAME",L"FileTransferService");
    CkHttpRequestW_AddHeader(req,L"X-EBAY-SOA-OPERATION-NAME",apiCall);
    CkHttpRequestW_AddHeader(req,L"X-EBAY-SOA-SECURITY-TOKEN",accessToken);
    CkHttpRequestW_AddHeader(req,L"X-EBAY-SOA-REQUEST-DATA-FORMAT",L"XML");
    CkHttpRequestW_AddHeader(req,L"X-EBAY-SOA-RESPONSE-DATA-FORMAT",L"XML");
    CkHttpRequestW_AddHeader(req,L"User-Agent",L"AnythingYouWant");

    pathToFileOnDisk1 = L"qa_data/ebay/uploadFileRequest.xml";
    success = CkHttpRequestW_AddFileForUpload(req,L"uploadFileRequest.xml",pathToFileOnDisk1);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpRequestW_lastErrorText(req));
        CkHttpW_Dispose(http);
        CkHttpRequestW_Dispose(req);
        CkStringBuilderW_Dispose(sbContentType);
        return;
    }

    pathToFileOnDisk2 = L"qa_data/ebay/BulkDataExchangeRequests.gz";
    success = CkHttpRequestW_AddFileForUpload(req,L"BulkDataExchangeRequests.gz",pathToFileOnDisk2);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpRequestW_lastErrorText(req));
        CkHttpW_Dispose(http);
        CkHttpRequestW_Dispose(req);
        CkStringBuilderW_Dispose(sbContentType);
        return;
    }

    // Add sub-headers for each file in the request.
    CkHttpRequestW_AddSubHeader(req,0,L"Content-Type",L"application/xop+xml; charset=UTF-8; type=\"text/xml\"");
    CkHttpRequestW_AddSubHeader(req,0,L"Content-Transfer-Encoding",L"binary");
    CkHttpRequestW_AddSubHeader(req,0,L"Content-ID",xmlUuid);
    CkHttpRequestW_AddSubHeader(req,1,L"Content-Type",L"application/octet-stream");
    CkHttpRequestW_AddSubHeader(req,1,L"Content-Transfer-Encoding",L"binary");
    CkHttpRequestW_AddSubHeader(req,1,L"Content-ID",fileAttachmentUuid);

    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpSReq(http,L"storage.sandbox.ebay.com",443,TRUE,req,resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkHttpRequestW_Dispose(req);
        CkStringBuilderW_Dispose(sbContentType);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    wprintf(L"Response status code = %d\n",CkHttpResponseW_getStatusCode(resp));

    xml = CkXmlW_Create();
    CkXmlW_LoadXml(xml,CkHttpResponseW_bodyStr(resp));

    if (CkHttpResponseW_getStatusCode(resp) != 200) {
        wprintf(L"%s\n",CkXmlW_getXml(xml));
        wprintf(L"Failed.\n");
        CkHttpW_Dispose(http);
        CkHttpRequestW_Dispose(req);
        CkStringBuilderW_Dispose(sbContentType);
        CkHttpResponseW_Dispose(resp);
        CkXmlW_Dispose(xml);
        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>

    wprintf(L"%s\n",CkXmlW_getXml(xml));

    // Get the "ack" to see if it's "Failure" or "Success"
    if (CkXmlW_ChildContentMatches(xml,L"ack",L"Success",FALSE)) {
        wprintf(L"Success.\n");
    }
    else {
        wprintf(L"Failure.\n");
    }



    CkHttpW_Dispose(http);
    CkHttpRequestW_Dispose(req);
    CkStringBuilderW_Dispose(sbContentType);
    CkHttpResponseW_Dispose(resp);
    CkXmlW_Dispose(xml);

    }