![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C++) Walmart - Update Inventory for an ItemNote: This example uses classes, properties, or methods discontinued in Chilkat v11.0.0 or greater. Demonstrates how to update inventory for an item.
#include <CkStringBuilderW.h> #include <CkAuthUtilW.h> #include <CkJsonObjectW.h> #include <CkHttpW.h> #include <CkXmlW.h> void ChilkatSample(void) { // --------------------------------------------------------------------------------------------------------- // Note: This example is deprecated. The Walmart API no longer uses the Signature method of authenticating. // Walmart now uses OAuth2. // --------------------------------------------------------------------------------------------------------- // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // Sends the following PUT request: // PUT https://marketplace.walmartapis.com/v2/inventory?sku={SKU} CkStringBuilderW sbUrl; bool success = sbUrl.Append(L"https://marketplace.walmartapis.com/v2/inventory?sku={SKU}"); int numReplaced = sbUrl.Replace(L"{SKU}",L"HOPP~904~51041"); const wchar_t *requestMethod = L"PUT"; // First we need to generate a signature for our request. // The signature needs to be re-generated for each new Walmart HTTP request. CkAuthUtilW authUtil; const wchar_t *wmConsumerId = L"WALMART_CONSUMER_ID"; const wchar_t *wmPrivateKey = L"WALMART_PRIVATE_KEY"; const wchar_t *jsonStr = authUtil.walmartSignature(sbUrl.getAsString(),wmConsumerId,wmPrivateKey,requestMethod); if (authUtil.get_LastMethodSuccess() != true) { wprintf(L"%s\n",authUtil.lastErrorText()); 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 CkJsonObjectW json; json.Load(jsonStr); CkHttpW http; http.SetRequestHeader(L"WM_SVC.NAME",L"Walmart Marketplace"); http.SetRequestHeader(L"WM_QOS.CORRELATION_ID",json.stringOf(L"correlation_id")); http.SetRequestHeader(L"WM_SEC.TIMESTAMP",json.stringOf(L"timestamp")); http.SetRequestHeader(L"WM_SEC.AUTH_SIGNATURE",json.stringOf(L"signature")); http.SetRequestHeader(L"WM_CONSUMER.ID",wmConsumerId); http.put_Accept(L"application/xml"); // Note: Do not explicitly set the "Host" header. Chilkat will set it automatically. // The body of the PUT request will be XML that looks like this: // <?xml version="1.0" encoding="utf-8" ?> // <wm:inventory xmlns:wm="http://walmart.com/"> // <wm:sku>HOPP~904~51041</wm:sku> // <wm:quantity> // <wm:unit>EACH</wm:unit> // <wm:amount>235</wm:amount> // </wm:quantity> // <wm:fulfillmentLagTime>5</wm:fulfillmentLagTime> // </wm:inventory> // Build the XML request body: int originalAmount = 234; CkXmlW xmlBody; xmlBody.put_Tag(L"wm:inventory"); xmlBody.AddAttribute(L"xmlns:wm",L"http://walmart.com/"); xmlBody.UpdateChildContent(L"wm:sku",L"HOPP~904~51041"); xmlBody.UpdateChildContent(L"wm:quantity|wm:unit",L"EACH"); xmlBody.UpdateChildContentInt(L"wm:quantity|wm:amount",originalAmount + 1); xmlBody.UpdateChildContent(L"wm:fulfillmentLagTime",L"5"); wprintf(L"PUT request body:\n"); wprintf(L"%s\n",xmlBody.getXml()); wprintf(L"--\n"); // This is a simple PUT that can be sent w/ the PutText method. const wchar_t *xmlStr = http.putText(sbUrl.getAsString(),xmlBody.getXml(),L"utf-8",L"application/xml",false,false); if (http.get_LastMethodSuccess() != true) { wprintf(L"%s\n",http.lastErrorText()); return; } CkXmlW xml; xml.LoadXml(xmlStr); // A successful response should have a 200 response status if (http.get_LastStatus() != 200) { wprintf(L"%s\n",xml.getXml()); wprintf(L"Response Status Code: %d\n",http.get_LastStatus()); wprintf(L"Failed.\n"); return; } // A sample XML response is shown below.. wprintf(L"%s\n",xml.getXml()); wprintf(L"--\n"); wprintf(L"Success!\n"); // --------------------------------------- // 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>235</amount> // </quantity> // <fulfillmentLagTime>5</fulfillmentLagTime> // </inventory> } |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.