How do I find the origin IP Address from a http request?

2,572 views
Skip to first unread message

kevin

unread,
Jan 2, 2011, 10:21:38 PM1/2/11
to golang-nuts
I'm trying to get the origin of a http request. I don't know exactly
how http communication works, but the address of the origin of the
request must be included in there somewhere, because otherwise you
wouldn't be able to respond. Is there any way to do this using the
http package?

What I was trying is below. However all the information that I get
from the request is &{GET / / HTTP/1.1 1 1 map[] 0x7f574c52a030 -1 []
false localhost:8085 Go http package map[] map[]}. This has the
destination "localhost:8085", but I can't figure out how to get the
origin. Are the destination and origin the same because I'm using
localhost to try this example? Is there another way of doing this?

// server.go ----------------------------------------------------
package main
import (
"fmt"
"http"
)
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8085", nil)
}
func handler(w http.ResponseWriter, r *http. Request) {
fmt.Printf("r: %v\n", r)
}

// client.go ------------------------------------------------------
package main
import (
"http"
)
func main() {
http.Get("http://localhost:8085")
}
Message has been deleted

kevin

unread,
Jan 3, 2011, 12:10:38 AM1/3/11
to golang-nuts
Thank you. That is exactly what I was looking for.

On Jan 2, 7:43 pm, gary b <gary.b...@gmail.com> wrote:
> You can get the IP address using RemoteAddr() in ResponseWriter.  Seehttp://golang.org/pkg/http/#ResponseWriterfor the documentation.

caleb.a....@gmail.com

unread,
Nov 15, 2017, 12:39:29 AM11/15/17
to golang-nuts
Can I get that answer from you? It has been deleted and I'm in need of it

Tamás Gulácsi

unread,
Nov 15, 2017, 12:58:45 AM11/15/17
to golang-nuts
Reply all
Reply to author
Forward
0 new messages