Search code examples
grailscookiesresponseinterceptorgrails3

Grails 3 interceptor - unable to set cookie in 'after' method


In my Grails 3.2 application I found out that cookies set in after and afterView methods are not set.

My (simplified) controller:

class MyInterceptor {

    boolean before() {
        Cookie c = new Cookie('before', 'ok')
        response.addCookie c
        true
    }

    boolean after() {
        Cookie c = new Cookie('after', 'ok')
        response.addCookie c
        true
    }

    void afterView() {
        Cookie c = new Cookie('afterview', 'ok')
        response.addCookie c
    }
}

Only before cookie is returned in set-cookie response header.

Am I missing any kind of limitations of response attribute in such methods?


Solution

  • The issue is when the after* method are invoked the controller or view may have already written to the response such that the response has been committed and it is too late to add any headers or cookies.

    This is not a limitation of Grails as such but the just the way servlet containers work.