How to get incoming IP address in Spray framework

The problem was in configuration, this is not documented well. Adding this:

# spray-can config
spray.can {
  server {
    remote-address-header = on
  }
}

forces spray to add remote IP header to the main headers. Address header will have name Remote-Address.


If you are using spray routing, then there is a directive for extracting client ip called clientIP =) To use it just write:

(path("somepath") & get) {
  clientIP { ip =>
    complete(s"ip is $ip")
  }
}

more then simple, but you need still need to add explicit configuration to get IP from request. And a little comment, maybe i didn't get something but in spray there is no implicit request. Actually incoming request percolates through your routing structure, if you take a look into the routing library you'll see that route is just an alias: type Route = RequestContext => Unit. So if you need to get access to the context at some point just write:

(path("somepath") & get) {
  clientIP { ip => 
    reqCont => reqCont.complete(s"ip is $ip")
  }
}

But remember about static route part and dynamic part.