function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
John T.John T. 

Download Attachments via Chatter REST Api

Hi Guys,

 

I'm having a problem downloading attachments using Chatter Rest API. When I tried to get the feed item response I get the download URL and used the url for my download. I'm having an error SESSION is invalid. What I want is get the attachments link and view it in my browser page. 

Thomas CookThomas Cook

The link today for clients that can pass an OAuth header when they make the request, but unfortunately there is not a good answer now for web clients that want to drop the link in an anchor tag.  We're working improving this for web clients in a future release.

John T.John T.

Are there any other ways to get the download link or preview link. What I want is I'm creating a hybrid application and I want to link the attachment like pdf to preview in my mobile device. Any advice would be great. Thanks.

Thomas CookThomas Cook

The app that downloads the attachment will need to authenticate with Salesforce, or if you've already authenticated, you'll need to pass the OAuth request header when trying to retrieve the content at the download URL:

 

Authorization: OAuth ...<token>...
Thomas CookThomas Cook

I should have made it clear in my previous post that you'll always need to provide the OAuth header in the request for the download link for it to work.  If you don't already have an OAuth token to pass, then you'll need to authenticate via an OAuth flow in order to get a token.

John T.John T.

Hi Thomas,

 

What I did is I attached the token in the href link like:

<a href="http://instanceUrl.salesforce.com//services/data/v23.0/chatter/files/06990000000LsZXAA0/content?versionNumber=1?oauth_token=token"></a>

 

I'm not sure if this is the right thing to do or shoud I still need to execute a Get request? How should I do this?

 

Thanks,

remyaMohananremyaMohanan

Hi John,

 

Did you get a way to get this working? Even I am trying to incorparate download attachment feature. I created the rest api which  takes the download url and instance url. But its throwing 404 error.

 

 

Thanks,

Remya

John T.John T.

Hi Remya,

 

What I did is I created a workaround for downloading attachment by using a php what I did is I passed paramters to the php download the attachment and render it as image or pdf.

 

Cheers,

remyaMohananremyaMohanan

Thanks alot John. But are you using the same url which we get from the response as downloadUrl. which is something like this:

/services/data/v23.0/chatter/files/069C0000000qbOhIAI/content?versionNumber=1

 

I am trying to create a rest api using this url and the instance url. But I am getting an error.

 

Thanks,

John T.John T.

Yep, that is exactly what I did. What exactly is the error?

remyaMohananremyaMohanan

Hi,

Finally I am getting a binary file response using the download url and I am able to get the download window. But the problem here is that even after setting the contentType with mime type I am not getting the file extension and I am getting a .part file. How are you setting the content type?

Below is the service code:

 

objHttpServletResponse.setContentType(mimeType);

//here i have to manually set the file extension according to mime type.

if (mimeType.equals("application/vnd.ms-excel")){
            fileExt="xls";   

}

objHttpServletResponse.setHeader("Content-Disposit

ion","attachment; filename=\""+filename+"."+fileExt+"\"");