Mohammed Shaheen MK
Mohammed Shaheen MK

Reputation: 1219

How to access cookie in grails2.2.5 service

I can't get cookie value from grails service, i am using code like this

g.cookie(name: 'cookieName')

I got an error

Unable to resolve g

How can i fix this

Upvotes: 0

Views: 128

Answers (2)

Koloritnij
Koloritnij

Reputation: 1217

You can receive current request in service: WebUtils.retrieveGrailsWebRequest().getCurrentRequest() And there will be field cookies as Array

Upvotes: 0

Tuomas Valtonen
Tuomas Valtonen

Reputation: 508

Most of the grails' Controller entities are not exposed to services, for a reason: it does not belong there. Same reason why you can't use message(code: ..) in a service. Also, cookies exists whenever request exists which is not something that you can have in a service. Some background job could call your service for example. Your BootStrap could make a service call when application starts. Its simply not on the http-layer.

You could pass your request (HttpServletRequest) to your service in a method call and then in the service use getCookies() or similar method, or you can do all http-related stuff in a Controller.

Upvotes: 1

Related Questions