jsongoogle-apps-scriptsmartsheet-api

Adding rows with smartsheet API in Google Apps Script


I'm having the same difficulty as user:itsallgood in this link--I get the following error when I submit the below request to add a row.

{"errorCode":1008,"message":"Unable to parse request. The following error occurred: Request body must be either a JSON object or JSON array."}

I believe the data is formatted correctly, and I have validated that it is valid JSON at jsonformatter.curiousconcept.com.

var params = {  
   "headers":{  
      "authorization":"Bearer <<removed for public posting>>"
   },
   "contentType":"application/json",
   "method":"PUT",
   "body":[  
      {  
         "toBottom":true,
         "cells":[  
            {  
               "columnId":4209581015492484,
               "value":"New row"
            }
         ]
      }
   ]
}
var response = UrlFetchApp.fetch("https://api.smartsheet.com/2.0/sheets/<<sheet key removed>>/rows", params)

The answer provided to itsallgood is that the problem is due to a bug with the API 1.1 endpoint and that 2.0 will solve it. However, I am using the API 2.0 endpoint and still getting the error.

If someone can help solve this riddle, I'll be much obliged!


Solution

  • Here's a complete example:

    function addRow(){
    var url ="https://api.smartsheet.com/2.0/sheets/5670346721388420/rows";
    
    var data ={
            "toBottom": true,
            "cells": [
                {
                    "value": "New data",
                    "columnId": 5759377954105220
                }
            ]
        };
    
    var options ={
            "headers": {
                "authorization": "Bearer ll352u9jujauoqz4fstvsae05"
            },
            "method": "post",
            "contentType": "application/json",
            "payload": JSON.stringify(data)
        };
    Logger.log("About to call: " + url);
    
    var response = UrlFetchApp.fetch(url, options);
    
    Logger.log("Response: " + response);
    }