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

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.


#node-js #json #rest

4 Likes16.75 GEEK