+ Start a Discussion
Sujata Tiwari 3Sujata Tiwari 3 

how to set cache control at header level in web service?

Hi,
I tried doing this :
request.setHeader('Cache-Control','public,max-age=43200');
but still when I checked response in rest api. I can't see my value at max-age, it's still displaying like this :
Cache-Control: no-cache,must-revalidate,max-age=0,no-store,private

Please help me with this on how to proceed further on the same.

Thanks in advance..!!
Asif Ali MAsif Ali M
Set the value in RestResponse not in RestRequest then your api response will show the correct value.
RestResponse res = RestContext.response;
res.setHeader('Cache-Control','public,max-age=43200');

 
Sujata Tiwari 3Sujata Tiwari 3
Hi Asif,

Thanks for your rpl. I tried this :

RestResponse res = RestContext.response;        
//httpResponse response = new httpResponse();  
 res.setHeader('Cache-Control','public,max-age=43200');

But the above code is throwing some error :
Method does not exist or incorrect signature: void setHeader(String, String) from the type System.RestResponse.

Please help me with the same.

Thanks
Asif Ali MAsif Ali M
I thought your webservice is a REST service so gave that code.
If you are using httpResponse() then try the below code
response.setHeader('Cache-Control','public,max-age=43200');

 
Sujata Tiwari 3Sujata Tiwari 3
Hi Asif,

Thanks for your help.
But it is working perfectly for me. Instead of setHeader I used addHeader() and it worked.

Appreciate your response..!!:)