I'm trying to create a http server in go, when I run the code, I'm getting the output as
Listening on localhost:8080
But when I use curl localhost:8080 to test it I'm getting DNS error
The below code starts an HTTP server, listens on port 8080 incoming requests.
package main
import (
"fmt"
"html"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
})
log.Println("Listening on localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
This is the output I get when I run curl localhost:8080
$ curl localhost:8080
<HTML><HEAD>
<TITLE>Network Error</TITLE>
</HEAD>
<BODY>
<FONT face="Helvetica">
<big><strong></strong></big><BR>
</FONT>
<blockquote>
<TABLE border=0 cellPadding=1 width="80%">
<TR><TD>
<FONT face="Helvetica">
<big>Network Error (dns_unresolved_hostname)</big>
<BR>
<BR>
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica">
Your requested host "localhost" could not be resolved by DNS.
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica">
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica" SIZE=2>
<BR>
For assistance, contact your network support team.
</FONT>
</TD></TR>
</TABLE>
</blockquote>
</FONT>
</BODY></HTML>
Looks like its an DNS issue, I'm trying to setup the http server in a VM, how can I fix this error?
CodePudding user response:
One guess is your http request is going through a proxy. You can unset the HTTP_PROXY
variable and try again.
Otherwise, try using 127.0.0.1
or the IP address for your machine. :8080
means the HTTP server is binding to all IPs.