javascriptxmlgoogle-apps-scriptpayloadwalmart-api

Unable to update feed using Walmart API


var data = {
"file": "<InventoryFeed xmlns=\"http://walmart.com/\">\n    <InventoryHeader>\n        <version>1.4</version>\n    </InventoryHeader>\n    <inventory>\n        <sku>JW00726</sku>\n        <quantity>\n            <unit>EACH</unit>\n            <amount>25</amount>\n        </quantity>\n    </inventory>\n    <inventory>\n        <sku>JW00663</sku>\n        <quantity>\n            <unit>EACH</unit>\n            <amount>20</amount>\n        </quantity>\n    </inventory>\n</InventoryFeed>\n"
  };
  
  var options = {
    "method" : "POST",
    "headers": {
        "Authorization": "Basic "+Global_Auth,
        "WM_QOS.CORRELATION_ID": Global_CORRELATION_ID,
        "WM_SVC.NAME": Global_SVC_NAME,
        "WM_SEC.ACCESS_TOKEN":GetAccessToken(),
        "WM_CONSUMER.CHANNEL.TYPE": "#",
        "Accept": "application/json",
        "mimeType": "multipart/form-data",
        "Content-Type": "application/x-www-form-urlencoded"
      },
    "payload" : data,
    "muteHttpExceptions" : true
  };
  var url = "https://marketplace.walmartapis.com/v3/feeds?feedType=inventory";
  var response = UrlFetchApp.fetch(url, options);
  var res = JSON.parse(response.getContentText());

in API response getting 200 (ok) response but values are not updating into Walmart feed.


Solution

  • When I searched about the endpoint of https://marketplace.walmartapis.com/v3/feeds?feedType=inventory of Walmart API, I found 2 patterns.

    1. It seems that this is the official document. Ref
    2. It seems that this is "Walmart Partner Apis Prod_Publish" at Postman. Ref

    In this answer, I would like to propose an answer using above 2 documents.

    Pattern 1:

    In this pattern, the official document is used. In this case, the data is sent with multipart/form-data. When your script is modified, it becomes as follows. The data is used from your script.

    Modified script:

    var data = `<InventoryFeed xmlns="http://walmart.com/"><InventoryHeader><version>1.4</version></InventoryHeader><inventory><sku>JW00726</sku><quantity><unit>EACH</unit><amount>25</amount></quantity></inventory><inventory><sku>JW00663</sku><quantity><unit>EACH</unit><amount>20</amount></quantity></inventory></InventoryFeed>`;
    var options = {
      "method": "POST",
      "headers": {
        "Authorization": "Basic " + Global_Auth,
        "WM_QOS.CORRELATION_ID": Global_CORRELATION_ID,
        "WM_SVC.NAME": Global_SVC_NAME,
        "WM_SEC.ACCESS_TOKEN": GetAccessToken(),
        "WM_CONSUMER.CHANNEL.TYPE": "#",
        "Accept": "application/json",
      },
      "payload": { "file": Utilities.newBlob(data, "text/xml") }, // or "application/xml" instead of "text/xml"
      "muteHttpExceptions": true
    };
    var url = "https://marketplace.walmartapis.com/v3/feeds?feedType=inventory";
    var response = UrlFetchApp.fetch(url, options);
    var res = JSON.parse(response.getContentText());
    

    Pattern 2:

    In this pattern, "Walmart Partner Apis Prod_Publish" at Postman is used. In this case, the data is sent with application/json. And the sample curl is as follows.

    curl --location --request POST 'https://marketplace.walmartapis.com/v3/feeds?feedType=inventory' \
    --header 'WM_SVC.NAME: Walmart Marketplace' \
    --header 'WM_QOS.CORRELATION_ID: test' \
    --header 'Accept: application/json' \
    --header 'WM_SEC.ACCESS_TOKEN: {{token}}' \
    --header 'Content-Type: application/json' \
    --data-raw '{"InventoryHeader":{"version":"1.4"},"Inventory":[{"sku":"1068155","quantity":{"unit":"EACH","amount":"10"}},{"sku":"10210321","quantity":{"unit":"EACH","amount":"20"}}]}'
    

    This is converted to Google Apps Script.

    Sample script:

    var data = {"InventoryHeader":{"version":"1.4"},"Inventory":[{"sku":"JW00726","quantity":{"unit":"EACH","amount":25}},{"sku":"JW00663","quantity":{"unit":"EACH","amount":20}}]};
    var options = {
      "method": "POST",
      "headers": {
        "Wm-Qos.Correlation-Id": Global_CORRELATION_ID,
        "Wm-Svc.Name": Global_SVC_NAME,
        "Wm-Sec.Access-Token": GetAccessToken(),
        "Accept": "application/json",
      },
      "contentType": "application/json",
      "payload": JSON.stringify(data),
      "muteHttpExceptions": true
    };
    var url = "https://marketplace.walmartapis.com/v3/feeds?feedType=inventory";
    var response = UrlFetchApp.fetch(url, options);
    console.log(response.getContentText())
    var res = JSON.parse(response.getContentText());
    

    Note:

    References: