2

I am trying to create a resumable upload session using drive rest API in Salesforce Apex.

As per the documentation the 3 steps needed to be followed are

  1. Start a resumable session
  2. Save the resumable session URI
  3. Upload the file

But i am not able to retrieve the Location header from the response. Even i tried the request from the postman rest client, it is having the same problem.

Code :

String body='{ "name" : "'+ filename+'",'+'"parents": ["0B3fYScqCn4pyWGRZVUIwWnNIbDg"] }';
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('https://www.googleapis.com/drive/v3/files?uploadType=resumable');
req.setHeader('Authorization', 'Bearer ' +accessToken);
system.debug('###accessToken'+accessToken);
req.setHeader('Content-Type', 'application/json;charset=UTF-8');
req.setHeader('Content-length', String.valueOf(body.length()));
req.setHeader('X-Upload-Content-Type',fileType);
req.setHeader('X-Upload-Content-Length',String.valueOf(fileSize));
req.setBody(body);
req.setMethod('POST');
//req.setTimeout(60*1000);
HttpResponse resp = http.send(req);
system.debug('@@@fileSize'+fileSize);
system.debug('@@@@@@@---'+resp.getbody());
system.debug('@@@@@@@---'+resp.getHeader('Location')); //returning null
for(String str : resp.getHeaderKeys()){

   system.debug('@@@@@@@---str:'+str+':: '+resp.getHeader(str));
   //no header with location /Location
}

Response :

{
"kind": "drive#file",
"id": "0B3fYScqCn4pyaGRYN214MnpiV2s",
"name": "Untitled",
"mimeType": "application/octet-stream"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.