3
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 Answers1

6

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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Great, This solution works phenomenal. Thank you very much!!! – JAIMIN VAGHANI Apr 20 '21 at 13:00
  • @JAIMIN VAGHANI Thank you for replying and testing it. I'm glad your issue was resolved. Thank you, too. – Tanaike Apr 20 '21 at 13:16
  • @Tanaike You're answer is amazing, the documentation on Walmart's official website is horrible, this saved me so much time. – ricks Jul 30 '21 at 14:03
  • I'm a little confused. Walmart Partner Apis Prod_Publish is a specific version or implementation of official apis? – habib May 30 '23 at 14:31
  • @habib I have to apologize for my poor English skill. Unfortunately, I cannot understand your question of `I'm a little confused. Walmart Partner Apis Prod_Publish is a specific version or implementation of official apis?` in your comment. Can I ask you about the detail of it? – Tanaike May 30 '23 at 23:27
  • @Tanaike sorry for late response patter1 uses a file to send data while pattern 2 uses josn to send data. you mentioned pattern1 as official document then pattern2 is non official? how is it possible that we can also send json other than file and the official documentation don't say anything about that? who wrote that "Walmart Partner Apis Prod_Publish"? – habib Jun 15 '23 at 12:21
  • @habib Thank you for replying. I have to apologize for my poor English skill again. From your reply, I cannot still understand your 1st question about `I'm a little confused. Walmart Partner Apis Prod_Publish is a specific version or implementation of official apis?`. About your 3 additional questions, I cannot answer them because I cannot understand them. But, I would like to support you. When I could correctly understand your all 4 questions, I would like to answer them. I deeply apologize that I cannot understand your 4 questions soon. I would like to study more. – Tanaike Jun 15 '23 at 12:29
  • @Tanaike no need to apologize, I think I'm not asking clearly. forget all things here is the question about v3/feeds?feedType=inventory. In the official docs, it says that we have to attach a file that contains the inventory data, right? but the official documentation doesn't tell us that we can also pass json instead of file? why is that so? – habib Jun 15 '23 at 13:54
  • @Tanaike can you also please take a look at this question too? https://stackoverflow.com/questions/76482385 I also contacted you via email to check that question – habib Jun 15 '23 at 13:57