Node Google Docs REST API Batch update invalid JSON payload/missing documentId

Node Google Docs REST API Batch update invalid JSON payload/missing documentId

When attempting to perform a batch update and I receive a Invalid JSON payload error; and trying to stringify JSON gives a 'missing documentId' error. The problem isn't with OAuth, the token or document scope as these are all correct and functioning (scope was changed from the example's read only to the full document scope).

When attempting to perform a batch update and I receive a Invalid JSON payload error; and trying to stringify JSON gives a 'missing documentId' error. The problem isn't with OAuth, the token or document scope as these are all correct and functioning (scope was changed from the example's read only to the full document scope).

Since there isn't an example for Google's new batch update API in node I have been having significant issues with batch update. After some troubleshooting with the batchUpdate constructor I have narrowed down my issue to (potentially) a larger issue with the API url constructor, or my syntax is wrong (or both.) Or I am missing steps to create the appropriate objects for the API call (no documentation is present for these tasks)

Inside a callback after successful get document as per google node quickstart guide (mostly)

    let offset = startIndex + 12
    let updateObject = {
      documentId:doc_id,
      requests:
        [{
          insertTextRequest : 
            {
              text : 'John Doe',
              location : {
                index : offset
              }
            }
        }]
      } 
    docs.documents.batchUpdate(updateObject,function(e,r){
      console.log(e)
      console.log(r)
    }

Google API response

'Invalid JSON payload received. Unknown name "requests[insertText][location][index]": Cannot bind query parameter. Field \'requests[insertText][location][index]\' could not be found in request message.\nInvalid JSON payload received. Unknown name "requests[insertText][text]": Cannot bind query parameter. Field \'requests[insertText][text]\' could not be found in request message.',
   domain: 'global',
   reason: 'badRequest' } ] }

Response after trying JSON.stringify(updateObject) - truncated

Error: Missing required parameters: documentId
at node_modules\googleapis-common\build\src\apirequest.js:114:19
at Generator.next (<anonymous>)

My best guess is some kind of google voodoo magic needs to occur for the API to properly encode the JSON object for the request to succeed.

  • Changing the array of a single request to an object had no effect on the above.
  • Using single/double quotes for the request object parameter names/string variable had no effect.
  • Document ID is a string, works, just not shown in the code example.
  • Adding documentId field to the request had no effect.


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

How to Create a Fake REST API Server using Node.js

How to make a REST API Server using Node.js for testing your AJAX client side.

Build a REST API using Node.js, Express.js, Mongoose.js and MongoDB

Node.js, Express.js, Mongoose.js, and MongoDB is a great combination for building easy and fast REST API. You will see how fast that combination than other existing frameworks because of Node.js is a packaged compilation of Google’s V8 JavaScript engine and it works on non-blocking and event-driven I/O. Express.js is a Javascript web server that has a complete function of web development including REST API.

Creating a RESTful Web API with Node.js and Express.js from scratch

In this article, I’ll show you step by step how to create a RESTful Web API with Node.js and Express.js by building a simple and useful Todo API. This article assumes you have basic javascript knowledge and terminal using capabilities.