Unable to update feed using Walmart API

1.6k views Asked by At
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.

1

There are 1 answers

9
Tanaike On BEST ANSWER

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());
  • When multipart/form-data is requested with UrlFetchApp, it is not required to set the content type. It is automatically set with the boundary.
  • When I saw your script, data is sent as form with the content type of application/x-www-form-urlencoded. I thought that this might be the reason of your issue.

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:

  • "mimeType": "multipart/form-data" is not used in the request header.
  • In above scripts, it supposes that your values of data and the values in the request header are correct values for using the API. Please be careful this.

References: