I am looking at headers that are coming in, but no IP seems to be there:
HttpRequest(GET,http://127.0.0.1:8080/track/check,List(Accept-Language: uk-UA,
uk, ru, en-US, en, Encoding: gzip, deflate, sdch, User-Agent: Mozilla/5.0
(Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29
Safari/537.36, Accept: text/html, application/xhtml+xml, application/xml;q=0.9,
*/*;q=0.8, Connection: keep-alive, Host: 127.0.0.1:8080),EmptyEntity,HTTP/1.1)
This is a request I did from browser. Basically I am looking in:
path("check") {
get {
implicit request => {
val a = 5
}
}
} ~
Here request object doesn't have any information about the IP address. Any help is very appreciated. thanks.
Where, “X_FORWARDED_FOR” is the HTTP header field for identifying originating IP Address of the client who is connected to Internet and “REMOTE_ADDR” returns the IP address of the remote machine. The second method of getting an IP address is using a built-in functionality of ASP.NET.
According to Wikipedia, An Internet Protocol address (IP address) is a numerical label assigned to each device (e.g., computer, printer) participating in a computer network that uses the Internet Protocol for communication. An IP address serves two principal functions: host or network interface identification and location addressing.
The HttpRequest is a sealed class that enables ASP.NET to read the HTTP values sent by the client browser during a Web request. We access the UserHostAddress property of HttpRequest class to get the IP Address of the visitor.
Retrieving IP address. The best way to retrieve the current IP address of the client is via middleware using a component such as ip-address-middleware. This component can be installed via composer: To use it, register the middleware with the App, providing a list of trusted proxies (e.g. varnish servers) if you are using them.:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With