Unexpected behavior when updating document title using Google Docs API

I’m having trouble with the Google Docs API. I wrote a script to copy a document and change its title. But something weird is happening. Instead of updating the title, it’s replacing the entire document content with the request body parameters.

Here’s what my code does:

  1. Makes a copy of a document
  2. Tries to update the title of the new copy
  3. Opens the new document in the browser
function duplicateAndRenameFile(label, sourceDocId) {
  const token = getUserAuthToken();
  const newFileName = `Report - ${label} ${getCurrentDate()}`;

  fetch(`https://www.googleapis.com/drive/v3/files/${sourceDocId}/copy`, {
    method: 'POST',
    headers: { 'Authorization': `Bearer ${token}` },
  })
    .then(response => response.json())
    .then(data => {
      fetch(`https://www.googleapis.com/upload/drive/v2/files/${data.id}`, {
        method: 'PUT',
        headers: { 'Authorization': `Bearer ${token}` },
        body: JSON.stringify({ 'title': newFileName })
      });
      console.log(data);
      window.open(`https://docs.google.com/document/d/${data.id}/edit`);
    });
}

Any ideas on what I’m doing wrong? How can I fix this so it updates the title correctly?

I’ve encountered a similar issue before. The problem lies in how you’re making the update request. For title updates, you should be using the Drive API, not the upload endpoint.

Try modifying your code to use this endpoint instead:

https://www.googleapis.com/drive/v3/files/${data.id}

Also, make sure to set the ‘Content-Type’ header to ‘application/json’ in your request. Here’s a snippet of how the update part should look:

fetch(`https://www.googleapis.com/drive/v3/files/${data.id}`, {
  method: 'PATCH',
  headers: {
    'Authorization': `Bearer ${token}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ name: newFileName })
});

This should update only the title without affecting the document’s content. Remember to handle any potential errors in your fetch calls as well.

hey CharlieLion22, i had the same issue! the problem’s with the endpoint ur using. instead of the upload one, try this:

https://www.googleapis.com/drive/v3/files/${data.id}

use PATCH method and set Content-Type to application/json. shud fix it without messin with the doc content. good luck!

I’ve actually run into this exact problem before when working on a document management system for my company. The issue stems from using the wrong endpoint and method for updating the file metadata.

Instead of using the upload endpoint with PUT, you should use the regular Drive API endpoint with a PATCH request. This way, you’re only updating the metadata (like the title) without touching the file contents.

Here’s what worked for me:

fetch(`https://www.googleapis.com/drive/v3/files/${data.id}`, {
  method: 'PATCH',
  headers: {
    'Authorization': `Bearer ${token}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ name: newFileName })
})
.then(response => response.json())
.then(updatedFile => {
  console.log('File updated:', updatedFile);
  window.open(`https://docs.google.com/document/d/${updatedFile.id}/edit`);
})
.catch(error => console.error('Error updating file:', error));

This approach should solve your problem without overwriting the document content. Hope this helps!