I want to add this custom header to every response on my rest API:
"customHeader": "foo"
For this, I have created a grails interceptor that matches every controller and allows me to modify the request.
class FooInterceptor {
FooInterceptor() {
matchAll()
}
boolean before() { true }
boolean after() {
header 'customHeader', "foo" //first try
response.addHeader 'customHeader', "foo" //second try to do the same
response.setHeader 'customHeader', "foo" //third try, setHeader doesn't work either
true
}
void afterView() {
}
}
I have debugged and I can see that the after method is called after the controller respond:
respond([status:dodes.OK], [:])
I can see clearly that my interceptor is called and the addHader is not throwing any exception but my header is just not added to the final response.
My guess here is that maybe the grail's respond method somehow "locks" the response so the header can't be added after but I am not sure.
How can I add a header to every response on grails 3 using an interceptor?