google-api-python-client: HttpError 400 on uploading 0 bytes files, 416 on downloading 0 bytes files
It seems like the bug #241 and @Aran-Fey’s problem happened again when i trying to download and upload 0 bytes file using google drive api.
credentials = google.oauth2.credentials.Credentials(**token)
services_account = googleapiclient.discovery.build(API_SERVICE_NAME, API_VERSION, credentials=credentials)
fd= BytesIO()
media = googleapiclient.http.MediaIoBaseUpload(fd=fd, chunksize=128, mimetype='text/plain', resumable=True)
body = {'name' : 'foo.txt'}
services_account.files().insert(body=body, media_body=media, fields='id').execute()
throws
`googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/upload/drive/v2/files?fields=id&alt=json&uploadType=resumable
returned "Bad Request">`
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 3
- Comments: 17 (8 by maintainers)
Ah, I missed the download part of this. Re-opening.
We confirmed that other clients fetch 0-bytes files without error, so this must be an issue with the python client.
The empty upload succeeds once the
Content-Range
header is removed from thePUT
request. This is also what the Java client appears to do (https://github.com/googleapis/google-api-java-client/blob/326370e0ec4351aaea21da271692b2fb9585a28c/google-api-client/src/main/java/com/google/api/client/googleapis/media/MediaHttpDownloader.java#L242-L255)The initial
POST
to begin the resumable upload succeeds. ThePUT
fails with invalid argument, and the content-range seems the most suspect at the momentcontent-range: bytes 0--1/0
.We are running into this as well. Ignoring 416 does the job, but looks like a hack. We are using v2 though (google-api-python-client-1.11.0), as we can’t migrate easily to v3. @SurferJeffAtGoogle Looks like other people are having this issue as well, should we reopen this issue? Or is it fixed in v3 and older ones won’t be fixed?
@busunkim96 The error states Drive V2. (It’s generally recommended to use V3)
@9kmmr It looks like the API request is bad. Here are more samples for uploading files: https://developers.google.com/drive/api/v2/manage-uploads