0

I'm trying to download a file from Sharepoint Online to a Salesforce folder using Apex REST and I'm running into an issue where I can read and access the file just fine using the Postman REST client, however I'm not able to duplicate the same behavior in my Salesforce dev sandbox.

Code:

public class spOnline {
   public static void listGroups() {
     List<String> spo_sitenames = new List<String>();
     spo_sitenames.add('IT');
     String spo_file = 'test.txt';
     HttpRequest req = new HttpRequest();
     req.setMethod('GET');
     req.setHeader('Accept', 'application/json;odata=verbose');
     req.setEndpoint('callout:Sharepoint3/{group-id}/drive/root/children/'+ spo_file+'/content');
     HTTPResponse res = new Http().send(req);
     //  while (res.getStatusCode() == 302) {
     //      req.setEndpoint(res.getHeader('Location'));
     //      res = new Http().send(req);
     //  }
     Document doc = new Document(Name=spo_file, Body=res.getBodyAsBlob(), FolderId='{Salesforce folder-id}');
     insert doc;
     Blob headerValue= blob.valueOf(res.getBody());
     String body = EncodingUtil.base64Encode(headerValue);
   }

I get back a file with zero bytes in the Salesforce folder I'm targeting and I think this is because I'm building the file, but no actual content is flowing to it. When I posed this question to Microsoft, they indicated calling the '/content' path would cause a 302 redirect to the actual file. I'm not sure if Salesforce can handle these redirects automatically or not, but could someone clarify the normal expected behavior in these circumstances?

  • as an example you can handle response code, and if it is 302 -- make another callout for an actual file – kurunve Aug 6 '18 at 15:37
  • hi @kurunve, could you direct me to an example of how this would happen in Apex? – xorLogic Aug 6 '18 at 15:38
  • hmm, I have not worked with Sharepoint, so I can not reproduce your scenario to provide exact point. Question is the following -- what does not work from your commented code? check this as well - salesforce.stackexchange.com/questions/11126/… – kurunve Aug 6 '18 at 15:40
  • I do not get the contents of the file on Sharepoint Online in Salesforce, but I can if I use the same URL in Postman – xorLogic Aug 6 '18 at 15:41
  • That is because postman does redirect for you. In apex you need to do it manually. Please, recheck my previous comment – kurunve Aug 6 '18 at 15:47
1

Your code has already an answer for it. Sometimes web services returns a response with a redirect to an actual resource (or chain of redirects), with status 30x and Location (or other) header with new URL, which is exactly this scenario. Browser does it automatically (or Postman), but if in Apex or other code (or even curl) you need to perform all following calls explicitly.

In your code you have the following code commented

//  while (res.getStatusCode() == 302) {
//      req.setEndpoint(res.getHeader('Location'));
//      res = new Http().send(req);
//  }

It does exactly that trick which is mentioned above -- in case if response status is 302 -- it get new URL and does it as long as response status is 302. So, to solve your problem, the following code can be uncommented.

Similar problem is covered here - How do I handle a 302 redirect response to my HTTP callout?

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.