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
jon-wujon-wu 

Can I set HTTP response headers in Apex?

Can I set my own response headers besides cache and content-type for an <apex:page> or any other pages on the force.com platform?

 

I'd be interested in setting my own headers in general but in this instance I'm trying to proxy a download of a JSON document through Apex with some modifications before it's passed off to the client.

 

I can return the text just fine and force it to download by modifying the Content-Type but without the Content-Disposition header, I can't set the filename which is a non-starter in my case. If I can't get Apex / VF to do this I'll have to setup a proxy outside of Salesforce.

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
Ralph CallawayRalph Callaway

Not sure if this is what you're looking for, but you can set a file name by appending #FileName to the content-type.

 

For example heres my <apex:page> declaration for a page that generates an excel file for download

<apex:page contentType="application/vnd.ms-excel#LineItemExport.xls" cache="true">

 

Another gotcha I've seen is that you need to set cache="true" for downloads to work in IE.

All Answers

Ralph CallawayRalph Callaway

Not sure if this is what you're looking for, but you can set a file name by appending #FileName to the content-type.

 

For example heres my <apex:page> declaration for a page that generates an excel file for download

<apex:page contentType="application/vnd.ms-excel#LineItemExport.xls" cache="true">

 

Another gotcha I've seen is that you need to set cache="true" for downloads to work in IE.

This was selected as the best answer
jon-wujon-wu

This is awesome! Thanks so much. Is this documented somewhere? Just wondering since I couldn't find it even now that I'm looking for it.

 

Good tip on cache I'm sure that would have been hard to figure out.

Ralph CallawayRalph Callaway

Just took a look at the documentation and wik and there doesn't seem to be anything that references it.  Another consultant showed me this trick.  Not sure where he found it.

Saiprasad KyatamSaiprasad Kyatam

Hi,

1) can anyody help how to invoke VF page with the help of Apexcode?
2) When iam using http request object and setting endpoint as ('/apex/VFPage?id=something') it returning its markup language in response.getBody(). whereas i want to set my own response and send it in response.getBody(). How to do that?

Please help and thanks in advance.

Saiprasad KyatamSaiprasad Kyatam
If i use PageReference pg=new PageReference('/apex/myVfPage?id=something');
Blob b=pg.getContent();
It returning Visual markup language with empty values i want values with that please share if any body knows

thanks in advance