Set HTTP header in Drive request

2019-08-10 09:08发布

问题:

I want to detect conflict when updating a file to Drive. To accomplish this, it seems I have to set the If-Match header.

Currently, I update the doc to Google Drive with this one-liner:

mDriveFile = mService.files().update(mDriveFile.getId(), mDriveFile, byteContent).execute();

What is the simplest way to add the If-Match header in the request? Example in Files: update documentation does not tell how to set HTTP headers.

回答1:

You can get the headers from Update object, add your own and put them back before executing the HTTP request:

final Update update = mService.files().update(mDriveFile.getId(), mDriveFile, byteContent);
final HttpHeaders headers = update.getRequestHeaders();
headers.setIfMatch("TheETagStoredFromDownload");
update.setRequestHeaders(headers);
mDriveFile = update.execute();