沃尔玛上的批量项目设置端点不使用 JSON

时间:2021-07-29 22:04:42

标签: ruby-on-rails httprequest walmart-api

我正在尝试按照此 guide 和此 api documentation

向 Walmarts API 提交供稿

在他们的指南中说

<块引用>

使用 XML 或 JSON 格式的有效负载向 Walmart 发送项目提要

我正在发送这个 JSON

{
   "MPItemFeedHeader":{
      "version":"1.0",
      "sellingChannel":"mpsetupbymatch",
      "locale":"en"
   },
   "MPItem":[
      {
         "Item":{
            "productIdentifiers":{
               "productId":"042666047173",
               "productIdType":"UPC"
            },
            "ShippingWeight":2,
            "price":420.69,
            "sku":"RICKS-12345"
         }
      }
   ]
}

通过像这样的 POST 请求:

# Submits a feed to Walmart
# @param feed_data [Hash] data that will be submited with the feed
# @param type [String] Enum: "item" "RETIRE_ITEM" "MP_ITEM" "MP_WFS_ITEM" "MP_ITEM_MATCH" "MP_MAINTENANCE" "SKU_TEMPLATE_MAP" "SHIPPING_OVERRIDES" 
def submit_feed(feed_data, type)
  # To add a param to a multipart POST request you need to append the params to the URL
  endpoint = "https://marketplace.walmartapis.com/v3/feeds?feedType=" + type
  
  headers = self.api_client.headers.with_indifferent_access 

  uri = URI(endpoint)
  request = Net::HTTP::Post.new(uri)

  # Add Required Headers
  request['Authorization'] = headers["Authorization"]
  request['WM_SEC.ACCESS_TOKEN'] = headers["WM_SEC.ACCESS_TOKEN"]
  request['WM_QOS.CORRELATION_ID'] = headers["WM_QOS.CORRELATION_ID"]
  request['WM_SVC.NAME'] = headers["WM_SVC.NAME"]
  request['Accept'] = headers["Accept"]

  # Set form wants an array of arrays, basically the first item is the key and the second the value
  request.set_form([["file", feed_data]], 'multipart/form-data')
  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(request)
  end
end

Feed 成功提交,但当我检查状态时,这是响应:

 {
   "feedId":"6DCFAA97311140358D6D842488B24333@AQMBCgA",
   "feedStatus":"ERROR",
   "shipNode":null,
   "submittedBy":null,
   "feedSubmissionDate":1627595359155,
   "ingestionErrors":{
      "ingestionError":[
         {
            "type":"DATA_ERROR",
            "code":"ERR_EXT_DATA_0801001",
            "field":"IB",
            "description":"Unexpected character '{' (code 123) in prolog; expected '\u003c'\n at [row,col {unknown-source}]: [1,1]"
         }
      ]
   },
   "itemsReceived":0,
   "itemsSucceeded":0,
   "itemsFailed":0,
   "itemsProcessing":0,
   "offset":0,
   "limit":50,
   "itemDetails":{
      "itemIngestionStatus":[
         
      ]
   },
   "additionalAttributes":null
}

根据错误信息判断

<块引用>

prolog 中出现意外字符 '{'(代码 123);预期 '\u003c'\n 在 [row,col {unknown-source}]: [1,1]

似乎他们希望我发送一个 XML 文件,我不知道我做错了什么。

他们要求将数据作为多部分发送,因此我无法将 Content-Type 设置为 application/json

enter image description here

在请求中我有什么要告诉他们有效负载是 JSON 的吗?

1 个答案:

答案 0 :(得分:1)

我在另一个问题的 this answer 的帮助下想通了。

您最好参考 this 而非 Walmarts 官方文档

您需要向 "https://marketplace.walmartapis.com/v3/feeds 端点提交一个发布请求,并将您的类型附加到 url ?feedType=[something]

如果您向他们发送 JSON,您需要确保您的 "Content-Type""application/json"

您不需要像文档建议的那样多部分发送它,只需将整个 JSON 文件作为正文发送即可。

这是我用来完成这项工作的新 ruby​​ 代码:

# Submits a feed to Walmart
# @param feed_data [Hash] data that will be submited with the feed
# @param type [String] Enum: "item" "RETIRE_ITEM" "MP_ITEM" "MP_WFS_ITEM" "MP_ITEM_MATCH" "MP_MAINTENANCE" "SKU_TEMPLATE_MAP" "SHIPPING_OVERRIDES" 
def submit_feed(feed_data, type)
  # To add a param to a multipart POST request you need to append the params to the URL
  endpoint = "https://marketplace.walmartapis.com/v3/feeds?feedType=" + type

  uri = URI.parse(endpoint)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.post(uri.path, feed_data, self.api_client.headers)
end

如果 Walmart 的某个人正在研究这个问题,改进您的文档。

相关问题