Google Drive Upload and Convert CSV to Sheets Giving "Bad Request" Error Only if the File Doesn't Already Exist - google-drive-api

I am trying to upload a CSV file to Google Drive and convert it to a Google Sheet. The filename in question is "text.csv".
If I upload the file and specify the filename as text.csv in the meta data (as well as provide the mime-type to convert to Google Sheets) it works fine. The file created is named "test" (no extension).
If I run the upload again specifying to replace the file, I get a duplicate file named "test". That's because the meta data says the filename is "test.csv" and when I search for a file it doesn't find an existing ID, therefore no ID to use to replace the file.
But, if I upload and specify a filename of "test" and the file doesn't already exist, I get an error "Bad Request". Here is the request:
POST /upload/drive/v3/files?key=xxx&uploadType=multipart&supportsAllDrives=true HTTP/1.1
Accept: text/html
Host: www.googleapis.com
Content-Type: multipart/related; boundary=PART.BOUNDARY.1
Content-Length: 2267
Authorization: Bearer xxx
--PART.BOUNDARY.1
Content-Type: application/json
{"name":"test","mimeType":"application/vnd.google-apps.spreadsheet","parents":["root"]}
--PART.BOUNDARY.1
Content-Type:
"Amsterdam ",2
"Argentina ",4
"Aruba ",1
..... [example data truncated]
--PART.BOUNDARY.1--
The error returned is:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "badRequest",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
}
But, here's another piece of the pie.
If I upload it with filename test.csv, it will create the file named test (as a Google Sheet). And if I then upload again specifying file name "test" and to replace the file, it works fine and replaces the original Google Sheet with the new file.
It's only when I try to upload the file when it doesn't already exist and I am specifying a name like "test" with no extension that I get the error.
Edit 1:
I tried with a .txt document converting to Google Docs and the same issue occurs. If I try to name the file "testdoc" without the Google Doc file named "testdoc" already existing, it errors out with the Bad Request error.
I am using POST for a new upload and PATCH for a replace as well.
Edit 2:
From Google documentation for drive:
"Apps should specify a file extension in the name property when creating files with the API. For example, an operation to insert a JPEG file should specify something like "name": "cat.jpg" in the metadata."
So then I looked up extensions for Google Drive files. I tried using .gsheet as the extension and still got a "Bad Request".
So, it looks like you must use an extension on the filename in the meta data when creating a file (and converting to a Google Drive file type), but if you want to replace the file, you can specify just the filename with no extension, even though the file uploading and being converted has an extension.
This seems to mean that you need to know if the file already exists (without the extension, ie. the Google Drive file already converted) and specify the name (without an extension) to replace is.
Seems odd.

Thanks for all the clues. They led me to realize the content type of the body/payload (the original file) was blank when it should have been text/csv in this case. For some reason this wasn't getting picked up.
--PART.BOUNDARY.1
Content-Type: <------- This should be text/csv
"Amsterdam ",2
"Argentina ",4
Once the correct content-type for the payload was added it worked as it should. Again, thanks for pointing me in the right direction.

Related

Published https://docs.google.com/spreadsheets redirects to other URL (CSV data)

We auto-publish a Google Docs Spreadsheet (one tab as CSV). Google docs is providing a fixed URL that refers to the CSV. We import this CSV in another tool for product data import.
Suddenly this URL is redirected by Google Spreadsheet. If we go again in "File/Publish To The Internet" we can the same URL for that CSV.
Question: How can get the URL without redirection again?
Error: Source file
https://docs.google.com/spreadsheets/d/e/2PACX-1vTQsBEmvOwFwxORMqYg2N6LzzYqdqsdDCjxqsdqsdH72gdMCP4xrs1lsN37RO4h1-rjJsQ/pub?gid=501162839&single=true&output=csv doesn't exist (HTTPS : File not found ! (HTTP/1.0 307 Temporary Redirect)). Please check the source file path.
In short, the collection process needs to follow the Location header. Depending how you're getting the CSV this might be simple or a pain. I collect CSVs using curl so just adding the -L switch is sufficient to make sure the incoming files are the CSV we're looking for instead of the HTML that we were getting without -L. Without knowing what utility or process you're using to download the CSV I can't be more specific, unfortunately.

How load annotations.tsv file to Google Custom Search Engine?

Trying to upload an annotations.tsv file to CSE, the upload dialogue says "File uploading is not successful". That's all. There's no specific error and no apparent cause. Everything looks fine.
The file is called: annotations.tsv
It has headers on the first line: URL<tab>Label<tab>Score
The file includes lines of the form: *somedomain.com/*<tab>_cse_etc<tab>1
And: someotherdomain/*<tab>_cse_etc<1>
What could possibly be wrong?
The problem was that one of the url patterns was invalid.
Google CSE does not give explicit error messages. If the file's list of patterns contains any that are invalid, it just rejects the whole file. If there are any other problems with the file, it just rejects the whole file.
The invalid pattern was of the form *.domainName.*
Whereas these are valid patterns:
www.domainName.com/
www.domainName.com/*
subdomainName.domainName.com/dirName/*
*.domainName.com/*
This is not valid: *.domainName.*

Google Drive MimeType with charset=utf-16le

I have a file with
content-type: text/html; charset=UTF-16LE that I'm uploading to Google Drive via the REST API.
When I preview the file in Drive, it shows me the raw HTML output.
However, when I download and open the file (in Chrome), the output is garbled HTML rather than the actual browser view of the file.
In debugging the file in Drive, it's only setting the content-type to "text/html", and it's not inheriting the "charset=UTF-16LE" that I specify on upload.
Is there another way to specify charset for the file I'm uploading to Drive?
I don't have the solution but I think could help you.
If it doesn't induce data corruption of your file, you can make your text/html file as a string, with :
var myVarString = myVar.toString();
and then use this code to set yourself the charset :
Utilities.newBlob("").setDataFromString(myVarString, "UTF-8").getDataAsString("UTF-16");

Webservice - Multiple file output formats

I need to create one webservice, that is used to download an audio(wav) file from server by taking input one string id. If audio file is not present at server, i need to send error back in json format.
Now the questions is - How to give the extension for downloaded file. I have no prior idea whether response will be success or failure.
What i am doing now is after dowloading a file - i am changing extension of file using Contet-Type header in response according to audio/wav or application/json.
Is there any way to specify file name with extension from server only. i.e downloaded file with name and extension.
You could set:
Content-Disposition: attachment; filename=FILENAME
header on the server side setting a proper xyz.json or xyz.wav filename as described in RFC 2183.

change Google Drive mimeType on update file

I am trying to make a "Save as" to google drive functionality for my web app. The user is shown the Google Picker and chooses a file to save over. At this point the application is updating the content of the file successfully, but it doesn't change the mimeType of the file being overwritten.
The api reference specifies the following about the mimeType metadata:
"The MIME type of the file. This is only mutable on update when uploading new content [this is the case]. This field can be left blank, and the mimetype will be determined from the uploaded content's MIME type."
I am leaving it blank. In fact, I am doing a simple upload to PUT https://www.googleapis.com/upload/drive/v2/files/{fileId}. And the http request has the Content-Type header set to the mimeType of the file being uploaded. In this case (if this helps) is "application/vnd.ms-project". I also tried adding the uploadType=media parameter to the url, with no success.
I also tried making a multipart update using the Api playground. Sent the metadata content empty {} and the error message was "Unsupported content with type: application/vnd.ms-project". Funny it is unsuported because I did manage to create a new file with the same mimeType, with no problem.
What to do? Any sugestions?
You can't replace an existing native Docs file with a regular blob. The only way to update a docs file with a blob is switching on the Docs conversion which is not possible for a Microsoft Project file.