bvstone
bvstone

Reputation: 607

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

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.

Upvotes: 0

Views: 516

Answers (1)

bvstone
bvstone

Reputation: 607

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.

Upvotes: 1

Related Questions