Home > Back-end >  "Object reference not set to an instance of an object" error when trying to update a Produ
"Object reference not set to an instance of an object" error when trying to update a Produ

Time:11-24

I'm having trouble updating a product in 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

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".

I've been using the GET request to get the product's attributes. Then I update the attributes that need to be changed, and store it in data (which is an array of objects). Then I use the PUT request to send the data.

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. 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? What am I missing?

CodePudding user response:

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}`,
  },
  payload: raw,
  contentType: "application/json",
};

Reference:

  • Related