Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

C Web API Examples

Primary Categories

Facebook
GeoOp
Jira
PayPal
Peoplevox
QuickBooks

Shopify
Stripe
SugarCRM
Twitter
VoiceBase
Walmart
Xero
eBay
effectconnect

 

 

 

(C) Walmart - Get Inventory for an Item

Demonstrates how to get inventory for an item and parse the result.

Note: This example requires Chilkat v9.5.0.67 or greater.

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Win Mobile 5.0/Pocket PC 2003

Solaris C/C++

FreeBSD C/C++

OpenBSD C/C++

MinGW C/C++

#include <C_CkStringBuilder.h>
#include <C_CkAuthUtil.h>
#include <C_CkJsonObject.h>
#include <C_CkHttp.h>
#include <C_CkXml.h>

void ChilkatSample(void)
    {
    HCkStringBuilder sbUrl;
    BOOL success;
    int numReplaced;
    const char *requestMethod;
    HCkAuthUtil authUtil;
    const char *wmConsumerId;
    const char *wmPrivateKey;
    const char *jsonStr;
    HCkJsonObject json;
    HCkHttp http;
    const char *xmlStr;
    HCkXml xml;
    int quantity;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // Sends the following GET request:
    // GET https://marketplace.walmartapis.com/v2/inventory?sku={SKU}

    sbUrl = CkStringBuilder_Create();
    success = CkStringBuilder_Append(sbUrl,"https://marketplace.walmartapis.com/v2/inventory?sku={SKU}");
    numReplaced = CkStringBuilder_Replace(sbUrl,"{SKU}","HOPP~904~51041");

    requestMethod = "GET";

    // First we need to generate a signature for our request.
    // The signature needs to be re-generated for each new Walmart HTTP request.
    authUtil = CkAuthUtil_Create();

    wmConsumerId = "WALMART_CONSUMER_ID";
    wmPrivateKey = "WALMART_PRIVATE_KEY";
    jsonStr = CkAuthUtil_walmartSignature(authUtil,CkStringBuilder_getAsString(sbUrl),wmConsumerId,wmPrivateKey,requestMethod);
    if (CkAuthUtil_getLastMethodSuccess(authUtil) != TRUE) {
        printf("%s\n",CkAuthUtil_lastErrorText(authUtil));
        CkStringBuilder_Dispose(sbUrl);
        CkAuthUtil_Dispose(authUtil);
        return;
    }

    // The JSON returned by WalmartSignature contains the values to be used in the following
    // header fields: WM_SEC.AUTH_SIGNATURE, WM_SEC.TIMESTAMP, and WM_QOS.CORRELATION_ID
    json = CkJsonObject_Create();
    CkJsonObject_Load(json,jsonStr);

    http = CkHttp_Create();
    CkHttp_SetRequestHeader(http,"WM_SVC.NAME","Walmart Marketplace");
    CkHttp_SetRequestHeader(http,"WM_QOS.CORRELATION_ID",CkJsonObject_stringOf(json,"correlation_id"));
    CkHttp_SetRequestHeader(http,"WM_SEC.TIMESTAMP",CkJsonObject_stringOf(json,"timestamp"));
    CkHttp_SetRequestHeader(http,"WM_SEC.AUTH_SIGNATURE",CkJsonObject_stringOf(json,"signature"));
    CkHttp_SetRequestHeader(http,"WM_CONSUMER.ID",wmConsumerId);
    CkHttp_putAccept(http,"application/xml");

    // Note: Do not explicitly set the "Host" header.  Chilkat will set it automatically.

    xmlStr = CkHttp_quickGetStr(http,CkStringBuilder_getAsString(sbUrl));
    if (CkHttp_getLastMethodSuccess(http) != TRUE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkStringBuilder_Dispose(sbUrl);
        CkAuthUtil_Dispose(authUtil);
        CkJsonObject_Dispose(json);
        CkHttp_Dispose(http);
        return;
    }

    xml = CkXml_Create();
    CkXml_LoadXml(xml,xmlStr);

    // A successful response should have a 200 response status
    if (CkHttp_getLastStatus(http) != 200) {
        printf("%s\n",CkXml_getXml(xml));
        printf("Response Status Code: %d\n",CkHttp_getLastStatus(http));
        printf("Failed.\n");
        CkStringBuilder_Dispose(sbUrl);
        CkAuthUtil_Dispose(authUtil);
        CkJsonObject_Dispose(json);
        CkHttp_Dispose(http);
        CkXml_Dispose(xml);
        return;
    }

    // A sample XML response is shown below..
    printf("%s\n",CkXml_getXml(xml));
    printf("--\n");

    // To get the quantity:
    quantity = CkXml_GetChildIntValue(xml,"quantity|amount");
    printf("quantity = %d\n",quantity);

    // ---------------------------------------
    // Sample XML response
    // ---------------------------------------

    // <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    // <inventory xmlns="http://walmart.com/">
    //     <sku>HOPP~904~51041</sku>
    //     <quantity>
    //         <unit>EACH</unit>
    //         <amount>234</amount>
    //     </quantity>
    //     <fulfillmentLagTime>5</fulfillmentLagTime>
    // </inventory>


    CkStringBuilder_Dispose(sbUrl);
    CkAuthUtil_Dispose(authUtil);
    CkJsonObject_Dispose(json);
    CkHttp_Dispose(http);
    CkXml_Dispose(xml);

    }

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.