简体   繁体   English

尝试使用 ShipStation 的 API 更新产品时出现“对象引用未设置为对象实例”错误

[英]"Object reference not set to an instance of an object" error when trying to update a Product with ShipStation's API

I'm having trouble updating a product in Shipstation.我在 Shipstation 中更新产品时遇到问题。

I'm been using these 2 links to see how I should format the response:我一直在使用这两个链接来查看我应该如何格式化响应:

https://www.shipstation.com/docs/api/products/update/ https://www.any-api.com/shipstation_com/shipstation_com/docs/Products/_products_productId_/PUT https://www.shipstation.com/docs/api/products/update/ https://www.any-api.com/shipstation_com/shipstation_com/docs/Products/_products_productId_/PUT

I believe I'm following it correctly, but I always get a 500 error message saying "Object reference not set to an instance of an object".我相信我正确地遵循它,但我总是收到一条 500 错误消息,说“对象引用未设置为对象的实例”。

I've been using the GET request to get the product's attributes.我一直在使用 GET 请求来获取产品的属性。 Then I update the attributes that need to be changed, and store it in data (which is an array of objects).然后我更新需要更改的属性,并将其存储在data中(这是一个对象数组)。 Then I use the PUT request to send the data.然后我使用 PUT 请求发送数据。

This is the relevant code:这是相关代码:

function updateProducts(authString, data) {

  var baseProductUrl = `https://ssapi.shipstation.com/products/`;

  for(var d = 0; d < data.products.length; d++) { //for each product I'd like to update...

    var raw = data.products[d];
    raw = JSON.stringify(raw);

    var requestOptions = { 

      method: 'PUT',
      headers: {
        "Authorization": `Basic ${authString}`,
        "Content-Type": `application/json`,       
      },

      body: raw,
      redirect: 'follow'
    };

    var productUrl = `${baseProductUrl}${data.products[d].productId}`;
    UrlFetchApp.fetch(productUrl, requestOptions);
  }
}

This is what raw (the data I'm sending to ShipStation) looks like: The null values are simply what I receive from the GET request.这就是raw (我发送到 ShipStation 的数据)的样子: null 值就是我从 GET 请求中收到的值。 I'd prefer those attributes stay empty.我希望这些属性保持空白。

{"aliases":null,
"productId":123456789, //placeholder
"sku":"sku", //placeholder
"name":"UV Bulb - 1GPM - 10\"",
"price":19.99,
"defaultCost":null,
"length":2,
"width":2,
"height":13,
"weightOz":7,
"internalNotes":null,
"fulfillmentSku":null,
"active":true,
"productCategory":null,
"productType":null,
"warehouseLocation":null,
"defaultCarrierCode":null,
"defaultServiceCode":null,
"defaultPackageCode":null,
"defaultIntlCarrierCode":null,
"defaultIntlServiceCode":null,
"defaultIntlPackageCode":null,
"defaultConfirmation":null,
"defaultIntlConfirmation":null,
"customsDescription":"UV Bulb - 1GPM - 10\"", //attribute I'd like to update
"customsValue":9.99, //attribute I'd like to update
"customsTariffNo":null,
"customsCountryCode":"US",
"noCustoms":null,
"tags":null}

So does anyone have any hints, or has anyone used ShipStation's API before and done a PUT request?那么有没有人有任何提示,或者有没有人使用过 ShipStation 的 API 并完成了 PUT 请求? What am I missing?我错过了什么?

In your script, how about the following modification?在您的脚本中,如何进行以下修改?

From:从:

var requestOptions = { 

  method: 'PUT',
  headers: {
    "Authorization": `Basic ${authString}`,
    "Content-Type": `application/json`,       
  },

  body: raw,
  redirect: 'follow'
};

To:至:

var requestOptions = { 
  method: 'PUT',
  headers: {
    "Authorization": `Basic ${authString}`,
  },
  pyaload: raw,
  contentType: "application/json",
};

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM