Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grails response.addHeader or setHeader is not possible in filter once response.setContentType is touched in controller action

In my controller action i tried to set the contentType of the response as below.

class MyController {
   def rss = {
       response.setContentType('text/xml')
       //I even tried setHeader('contentType') and also render(contentType:'text/xml',...)
       render(template:'/displayRss', model:[:])
   }
}

In the filter i tried to set the header for all controller and action params

class Filter {
   def filters = {
      all(controller:'*', action:'*'){
         after = { 
            response.setHeader('Cache-Control', 'no-cache')
         }
      }
   }
}

Header is added for all other actions except for the action in which response contentType is modified. It seems like response.isCommitted() is returned as true in that action alone. Am i am not understanding the basic of rendering...?

Also this is not the case if i use render(view:'/abc') instead of view(template:'/abc', model:[:])

Thanks in advance

like image 832
Arvind Avatar asked Jan 03 '12 23:01

Arvind


1 Answers

It looks like rendering a template sends the template immediately when called, as it's showing up as already committed in the filter. Since it's committed the headers have already been sent and setting a header after the fact doesn't make sense, so the setHeader in the Filter gets ignored.

My guess is this is a peculiarity to rendering templates specifically, and in those cases you'll need to set the cache header in the controller action explicitly, or use view rendering instead.

like image 172
Aquatoad Avatar answered Nov 15 '22 06:11

Aquatoad