0

Goal is to update "OrderStatusID" of order to "1" using urlfetchapp with a put request via Apps Script.

function updateOrderStatus(){

  var private_key = "{private_key}";
  var merchant_token = "{merchant_token}";
  var secure_url = "{secure_url}";
  
  var body = JSON.stringify({"OrderStatusID": "1"});
  var url ="https://apirest.3dcart.com/3dCartWebAPI/v2/Orders/{orderID}";
  
  var options =  {
    "method" : "put",
    "headers" : {
      "Content-Type" : "application/json",
      "Content-Length" : body.length,
      "Accept" : 'application/json',
      "SecureURL" : secure_url,
      "PrivateKey" : private_key,
      "Token" : merchant_token
    },
    "body" : body,
    "muteHttpExceptions" : false,
  }
  try{
    var response = UrlFetchApp.fetch(url, options);
  }
  catch(err){
    Logger.log(err);
  }
  finally{
    Logger.log(response);
  }
}

Code throws error Exception: Attribute provided with invalid value: Header:Content-Length

Code altered to remove sensitive information.

Gary D
  • 1
  • 4
  • hm, is `body` the same as you presented in the example? Has been a while since I've done GAS thoroughly, but the code looks ok to me if the stringified body consists of single-octet chars. – Oleg Valter is with Ukraine Mar 09 '21 at 03:08
  • Does this answer your question? https://stackoverflow.com/a/26615965 – Aerials Mar 09 '21 at 09:29
  • 1
    @Aerials that leads to the proper answer. Content-Length calculates automatically. All I needed to do was remove that header and change "body" to "payload" and the code worked fine. Thank you for helping point me in the right direction. – Gary D Mar 10 '21 at 03:55
  • @GaryD I'm glad it helped you. Would you like to post an answer to this question? – Aerials Mar 15 '21 at 16:50
  • @Aerials, I added an answer, thank you. – Gary D Mar 17 '21 at 01:38

1 Answers1

0

Changing the name of the option "body" to "payload" resolved the issue.

The answer was found in this google issue tracker thread.

ek...@googlers.comek...@googlers.com #6Apr 11, 2016 07:03AM Status: Won't Fix (Not Reproducible) UrlFetchApp doesn't have an advanced parameter called 'contentLength'. The Content-Length header is automatically calculated based on the length of the payload passed in. The 'contentLength' advanced parameter you set in your sample is simply ignored by the backend and the length automatically calculated.

The Content-Length is automatically calculated from the payload. I thought it was supposed to be named body because the 3dCart API documentation was using body as the name of its json in the example.

Corrected Code:

function updateOrderStatus(){

  var private_key = "{private_key}";
  var merchant_token = "{merchant_token}";
  var secure_url = "{secure_url}";
  
  var body = JSON.stringify({"OrderStatusID": "1"});
  var url ="https://apirest.3dcart.com/3dCartWebAPI/v2/Orders/{orderID}";
  
  var options =  {
    "method" : "put",
    "headers" : {
      "Content-Type" : "application/json",
      //"Content-Length" : body.length,
      "Accept" : 'application/json',
      "SecureURL" : secure_url,
      "PrivateKey" : private_key,
      "Token" : merchant_token
    },
    "payload" : body,
    "muteHttpExceptions" : false,
  }
  try{
    var response = UrlFetchApp.fetch(url, options);
  }
  catch(err){
    Logger.log(err);
  }
  finally{
    Logger.log(response);
  }
}
Gary D
  • 1
  • 4