【问题标题】:Walmart API - payload mime type issue - bulk create/update items not workingWalmart API - 有效负载 mime 类型问题 - 批量创建/更新项目不起作用
【发布时间】:2017-01-12 03:59:57
【问题描述】:

我能够成功地对其他 Walmart API 端点(特别是 GET 请求端点)进行 API 调用,但批量项目端点(POST 请求)会导致错误,即使我似乎一切都正确。

这是对该端点的引用 https://developer.walmartapis.com/#bulk-createupdate-items


HTTP 响应元信息

500 内部服务器错误

xml

HTTP 响应

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:errors xmlns:ns2="http://walmart.com/">
    <ns2:error>
        <ns2:code>SYSTEM_ERROR.GMP_GATEWAY_API</ns2:code>
        <ns2:description>Couldn't determine the boundary from the message!</ns2:description>
        <ns2:info>System encountered some internal error.</ns2:info>
        <ns2:severity>ERROR</ns2:severity>
        <ns2:category>DATA</ns2:category>
        <ns2:causes/>
        <ns2:errorIdentifiers/>
    </ns2:error>
</ns2:errors>

HTTP 请求:

网址 https://marketplace.walmartapis.com/v2/feeds?feedType=item

方法发布

请求标头

WM_SVC.NAME: Walmart Marketplace
WM_CONSUMER.ID: {my consumer id key}
WM_QOS.CORRELATION_ID: {my arbitrary text key}
Content-Type: multipart/form-data
Accept: application/xml
WM_SEC.AUTH_SIGNATURE:{my jar-file-generated key}
WM_SEC.TIMESTAMP:{my jar-file-generated timestamp}

POST 负载(只是文本,没有参数的键)

<?xml version="1.0" encoding="UTF-8"?>
<MPItemFeed xmlns="http://walmart.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://walmart.com/ MPItem.xsd ">
  <MPItemFeedHeader>
    <version>2.1</version>
    <requestId>qqq</requestId>
    <requestBatchId>qqq1</requestBatchId>
  </MPItemFeedHeader>
  <MPItem>
    <sku>qqq</sku>
    <Product>
      <productName>QQQ 1-Foot S-Video Male to 2 S-Video Female Y Cable (CSV2F)</productName>
      <longDescription><![CDATA[<div class="productDescriptionWrapper"> QVS Premium S-Video Mini4 Male to Two Female Splitter Cable CSV2F A/V Device Cables <div class="emptyClear">
      </div>
      </div>]]></longDescription>
      <shelfDescription><![CDATA[QVS 1-Foot S-Video Male to 2 S-Video Female Y Cable (CSV2F)]]></shelfDescription>
      <shortDescription>QQQ 1-Foot S-Video Male to 2 S-Video Female Y Cable (CSV2F)</shortDescription>
      <mainImage>
        <mainImageUrl>http://images.antonline.com/img-main/500/037229400328.jpg</mainImageUrl>
      </mainImage>
      <productIdentifiers>
        <productIdentifier>
          <productIdType>Item ID</productIdType>
          <productId>46817049</productId>
        </productIdentifier>
      </productIdentifiers>
      <productTaxCode>2038710</productTaxCode>
      <Electronics>
        <brand>QQQ</brand>
        <ElectronicsCables>
        </ElectronicsCables>
      </Electronics>
    </Product>
    <price>
      <currency>USD</currency>
      <amount>12.34</amount>
    </price>
    <shippingWeight>
      <value>1.234</value>
      <unit>LB</unit>
    </shippingWeight>
  </MPItem>
</MPItemFeed>

注意事项:

我尝试在发出 http 请求时使用 Google 的 Advanced REST Client Application 以及 POSTMAN,以排除我的代码引起的问题。

我已经尝试调整所有想到的东西。

我让其他端点有 GET 请求工作,所以我知道这不是身份验证、IP、防火墙或类似的问题。

我知道 walmart 提供的 jar 文件生成的凭据(请参阅 文档https://developer.walmartapis.com/#jar-executable-recommended 的身份验证部分)必须提供您要发送到的网址。所以我知道这不是用 jar 文件生成错误密钥的问题。


假设:

  • 根据响应,我发送的数据或标头一定有问题。

无法根据消息确定边界!

听起来 xml 没有平衡标签,但我已经验证了这个 xml

  • 此端点的 walmart api 文档说使用标头 Content-Type: multipart/form-data;。所以我做了,导致失败响应。

但是使用这个标头对我来说似乎没有意义,因为有效负载正文是一个 xml 字符串。不应该是Content-Type: application/xml吗?我也试过了,但是上面的响应失败了(500,SYSTEM_ERROR.GMP_GATEWAY_API,无法从消息中确定边界!)

所以看来Content-Type: application/xml可以排除了。

  • walmart api 文档仅提供了如何更改产品 id 或 sku 的示例,而不是用于创建的示例。我假设我只是省略了特定于更新产品 id 或 sku 的 xml 标记,但该 xml 有效负载也会导致 500 响应。

我没有想法,还有其他人可以批量创建/更新项目吗? 有人有实际工作的示例代码吗?

【问题讨论】:

    标签: http-post walmart-api


    【解决方案1】:

    如果有人在使用 PHP 时遇到这个问题,并且即使在尝试了 OP 的解决方案后仍然遇到问题(例如,500 响应消息“无法从消息中确定边界! em>”或“系统遇到一些内部错误”,您可以使用 cURL 通过将CURLOPT_POSTFIELDS 作为数组传递来自动为您处理边界,如下所示:

    curl_setopt($c, CURLOPT_POSTFIELDS, ['file' => $xml]);
    

    【讨论】:

    • 在您的示例中,file 是否意味着边界将是“文件”?还有一个如何表示多部分?换句话说,你如何处理实际上有多个部分,而不仅仅是一个?
    【解决方案2】:

    问题在于它需要一个 HTTP 请求边界。

    由于 mime 类型是 multipart/form-data,它需要一些东西:

    1. 标题行应为Content-Type: multipart/form-data; boundary=qwerty,其中 qwerty 是您选择的任意字符串。
    2. payload body 需要以--qwerty 开头,以--qwerty-- 结尾

    【讨论】:

      【解决方案3】:

      这是一个工作请求的示例:

      POST /v2/feeds?feedType=inventory
      Content-Length: 750
      Content-Type: multipart/form-data; boundary=72c4c966adda8bba2e0b3ebc3176cc0c395dd8c8
      Host: marketplace.walmartapis.com
      Accept: application/xml
      Accept-Encoding: gzip, deflate, br
      User-Agent: <...>
      Accept-Language: en-US
      WM_CONSUMER.CHANNEL.TYPE: <...>
      WM_CONSUMER.ID: <...>
      WM_SEC.TIMESTAMP: <...>
      WM_SEC.AUTH_SIGNATURE: <...>
      WM_SVC.NAME: Walmart Marketplace
      WM_QOS.CORRELATION_ID: <...>
      
      --72c4c966adda8bba2e0b3ebc3176cc0c395dd8c8
      Content-Disposition: form-data; name="xml"
      Content-Length: 591
      
      <?xml version="1.0" encoding="UTF-8"?>
      <InventoryFeed xmlns="http://walmart.com/">
        <InventoryHeader>
          <version>1.4</version>
        </InventoryHeader>
        <inventory>
          <sku>sku</sku>
          <quantity>
            <unit>EACH</unit>
            <amount>1</amount>
          </quantity>
          <fulfillmentLagTime>5</fulfillmentLagTime>
        </inventory>
      </InventoryFeed>
      
      --72c4c966adda8bba2e0b3ebc3176cc0c395dd8c8--
      

      【讨论】:

        【解决方案4】:

        我通过这样格式化我的有效载荷解决了这个问题:

         $eol = "\r\n";
        $data = '';
        
        $mime_boundary="12345";
        
        $data .= '--' . $mime_boundary . $eol;
        $data .= '<?xml version="1.0"?>' . $eol . $eol;
        $data .= '<MPItemFeed xmlns="http://walmart.com/">' . $eol;
        $data .= '<MPItemFeedHeader>' . $eol;
        $data .= '<version>2.1</version>' . $eol;
        $data .= '<mart>WALMART_US</mart>' . $eol;
        // $data .= '<locale>en_US</locale>' . $eol;
        $data .= '</MPItemFeedHeader>' . $eol;
        
        
        
        $data .= '<MPItem>' . $eol;
        $data .= '<sku>UHP-8224-JH</sku>' . $eol;
        $data .= '<productIdentifiers>' . $eol;
        $data .= '<productIdentifier>' . $eol;
        $data .= '<productIdType>UPC</productIdType>' . $eol;
        $data .= '<productId>849849004721</productId>' . $eol;
        $data .= '</productIdentifier>' . $eol;
        $data .= '</productIdentifiers>' . $eol;
        $data .= '<MPProduct>' . $eol;
        $data .= '<productName>295 / 30 R 22 103Y  ANTARES MAJORIS M5  30 K MILES </productName>' . $eol;
        $data .= '<ProductIdUpdate>No</ProductIdUpdate>' . $eol;
        // $data .= '<SkuUpdate>No</SkuUpdate>' . $eol;
        $data .= '<category>' . $eol;
        $data .= '<Vehicle>' . $eol;
        //$data .= '<Vehicle>' . $eol;
        $data .= '<shortDescription>295 / 30 R 22 103Y  ANTARES MAJORIS M5  30 K MILES</shortDescription>' . $eol;
        $data .= '<brand>ANTARES</brand>' . $eol;
        $data .= '<mainImageUrl>https://www.stuff4crafts.com/media/catalog/product/4/3/437764.jpg</mainImageUrl>' . $eol;
        $data .= '<tireSize>2255516</tireSize>' . $eol;
        $data .= '</Vehicle>' . $eol;
        //$data .= '</Vehicle>' . $eol;
        $data .= '</category>' . $eol;
        $data .= '</MPProduct>' . $eol;
        $data .= '<MPOffer>' . $eol;
        $data .= '<price>203.99</price>' . $eol;
        $data .= '<MinimumAdvertisedPrice>203.99</MinimumAdvertisedPrice>' . $eol;
        $data .= '<ShippingWeight>' . $eol;
        $data .= '<measure>0.8000</measure>' . $eol;
        $data .= '<unit>lb</unit>' . $eol;
        $data .= '</ShippingWeight>' . $eol;
        $data .= '</MPOffer>' . $eol;
        $data .= '</MPItem>' . $eol;
        
        
        
        $data .= '</MPItemFeed>' . $eol;
        
        $data .= "--" . $mime_boundary . "--" . $eol . $eol; // finish with two eol's!!
        

        【讨论】:

          【解决方案5】:

          选择您的任意字符串,比如说 arbString,例如abcdefghijklmnop(任何不应在您的 XML 正文中重复的字符串)

          创建批量价格/库存的 XML 有效负载(文件输入流)。 将您的有效负载转换为字符串 FileString = Ioutils.toString("FIS",standardCharacterset.UTF_8) .

          现在追加 "--arbString/n/n"+FileString+"/n--arbString--"

          【讨论】:

            猜你喜欢
            • 2021-09-14
            • 2019-07-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-04-07
            相关资源
            最近更新 更多