Monday, 15 September 2014

Can't get client IP in Grails -



Can't get client IP in Grails -

i'm trying write simple grails controller homecoming location of user.

the location part works fine when hardcore in ip address. problem lies in getting user's ip address.

upon running below code, next error:

"no signature of method: helloworld.hellocontroller.getremoteaddr() applicable argument types: () values: []"

the error seems caused line:

def ipaddress = getremoteaddr()

i'm connecting through localhost:8090/helloworld/hello/index on machine running grails app , 192.168.0.10:8090/helloworld/hello.index on laptop.

rest of code:

class hellocontroller {

def geoipservice def index() { def ip = getipaddress() def location = geoipservice.getlocation(ip) render location.countryname + " " + location.city } def getipaddress(javax.servlet.http.httpservletrequest request) { // def ipaddress = getremoteaddr() def ipaddress = getremoteaddr() if (ipaddress && inetaddressvalidator.validator.isvalid(ipaddress)){ log.debug("remote ip address ::: " + ipaddress) homecoming ipaddress } ipaddress = request.getheader("x-forwarded-for") if(ipaddress && inetaddressvalidator.validator.isvalid(ipaddress)) { log.debug("remote ip address ::: " + ipaddress) homecoming ipaddress } ipaddress = request.getheader("client-ip") if(ipaddress && inetaddressvalidator.validator.isvalid(ipaddress)) { log.debug("remote ip address ::: " + ipaddress) homecoming ipaddress } homecoming ipaddress } }

any help much appreciated!

you can access current httpservletrequest grails controller via built-in request object. there's no need specify argument method, you've done. if change:

def getipaddress(javax.servlet.http.httpservletrequest request) { ...

to:

def getipaddress() { ...

you able utilize request.getremoteaddr() (or request.remoteaddr groovy's convenient accessor syntax).

grails ip

No comments:

Post a Comment