1

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.

1 Answer 1

1

How about this modification?

Modification points:

  • Modify the property of insertTextRequest to insertText.
  • About updateObject, please use the property of resource for putting the request body.

Modified script:

let offset = startIndex + 12;
let updateObject = {
    documentId: doc_id,
    resource: {
        requests: [{
            "insertText": {
                "text": "John Doe",
                "location": {
                    "index": offset,
                },
            },
        }],
    },
};
docs.documents.batchUpdate(updateObject, function(e, r) {
    if (e) {
        console.log(e);
    } else {
        console.log(r.data);
    }
});

Note:

  • If the error of status INVALID_ARGUMENT and message "Invalid requests[0].insertText: Index ### must be less than the end index of the referenced segment, ##." occurs, for example, please try to modify index to 1.
  • If the error occurs, please try to use the latest version of googleapis, because Docs API was added recently.
  • This modified script supposes that the access token includes the scope of https://www.googleapis.com/auth/documents and Docs API is enabled.

References:

If I misunderstood your question, I apologize.

Sign up to request clarification or add additional context in comments.

2 Comments

Worked! I tried with insertText when posting the question, but saw the documentation listed as insertTextRequest. I was missing the enclosing 'resource' object for requests. Thanks for your prompt reply!
@redcap3000 Thank you for replying. I'm glad your issue was resolved. Now I'm using Docs API with node.js by referring the sample script of Spreadsheet, because the official document is still growing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.