Get the IP Address of a HTTP Request
·
201 words
·
1 minute read
This post demonstrates how to get the IP address of incoming HTTP requests in Go. As a function, it attempts to use the X-FORWARDED-FOR
http header for code behind proxies and load balancers (such as on hosts like Heroku) while falling back to the RemoteAddr
if the header isn’t found.
Just as an example, we created an echo server (of sorts) below to reply to incoming requests with the requesting ip address in json form.
|
|
(ignore the printf
- it’s just to keep the output tidy)