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
Ron WildRon Wild 

Setting the http response status code on a visualforce page

I've been pouring over the docs and forums but haven't been able to determine if there's a way to set the http response code on a call to a visualforce page.


For example, I'd like to return 500 if I encounter an error in my Apex code and 200 if everything works.




If I understand your requirement right, you want to display errors in the VF page which are thrown in your Apex Class/Controller?

Try using the apex:messages component of VF and the Apex Messages class. So no, I don't know of any way to use the http response object to do this directly. But in your apex controller method, catch the proper error scenarios, and generate the
Apex Message to be displayed in the <apex:messages> tag in the page. 



Ron WildRon Wild

Thanks, but that's not what I'm looking for.   I'm setting up a Sites page that has to return a status code in the HTTP response header when called from another application.


The caller sends an http request, and if it receives http 200 it knows the work is done.  If it gets a different status code (e.g. 500 - internal server error) it tries again later.




Did you ever figure this out, Ron? I have the same need and have not been able to figure this one out yet.





Ron WildRon Wild

I haven't seen a solution yet.   I don't think it's supported yet.


If you'd like to vote on my Ideas posting, it might help to get the SF development team to add it as a new feature:





Hi Ron,


If in case you are still looing for a solutions (it seems to be around 2 yr since your last post).


We can now set the response header information by having a code similar to one below:-


Apexpages.currentPage().getHeaders().put('Content-type', 'text/calendar');


Hope it helps.



Anshul Verma

Saiprasad KyatamSaiprasad Kyatam
Hi Iam Sai,
1) I am looking for code where i can use httprequest obj setting endpoint like ('/apex/myVFpage') which returns some response done by that (myVFpage) page and get my response in response.getBody(). (I want to set my own customize response which is done by javascript)
2) How can I invoke Visualforce page through apex ( not by controller ).

Any guidance appreciated in advance thanks.