google / google-api-javascript-client

Google APIs Client Library for browser JavaScript, aka gapi.
Apache License 2.0
3.22k stars 1.06k forks source link

Gibberish returned from gapi.client.drive.files.get() using alt: 'media' if content includes non-English (charset?) letters #1188

Open thexs-dev opened 9 months ago

thexs-dev commented 9 months ago

I am using the following code to get the content of a text file uploaded (plain text with MimeType.JAVASCRIPT) to Google Drive.

let response = await gapi.client.drive.files.get({
  'fileId': "google-drive-file-id-goes-here", alt: 'media'
});
let content = response.body;

It gets the actual content in Google Drive if the content only includes English (charset?) letters

See example below for the word Pharmacy in other languages and what the content is showing between ()

How to fix this issue using the gapi.client.drive.files.get() function?

Thanks in advance for your support on this Fausto

adevine commented 6 months ago

There is what I would consider a bug in the /drive/v3/files/${fileID} endpoint that's hit when calling drive.files.get or drive.files.export in that the server is always returning a Content-Type of "text/plain;charset=UTF-8". This Stack Overflow answer, https://stackoverflow.com/a/63818644/1075909, explains how to work around this. Other related SO post, https://stackoverflow.com/questions/66799006/unwanted-content-type-text-plaincharset-utf-8-header-in-google-drive-api-res