Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go web server don't show html pages (show html code) #37474

Closed
SergeyVlasov opened this issue Feb 26, 2020 · 2 comments
Closed

go web server don't show html pages (show html code) #37474

SergeyVlasov opened this issue Feb 26, 2020 · 2 comments

Comments

@SergeyVlasov
Copy link

SergeyVlasov commented Feb 26, 2020

What version of Go are you using (go version)?

$ go version
1.14

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

xubuntu 18.04

go env Output
$ go env
export PATH=$PATH:/usr/local/go/bin

What did you do?

I write a simple web server by golang. I use gorilla package and i have a problem to show html body of web page. I have an html code instead of web page/ I tried mozilla and chrome (same result)
package main
import (
"html/template"
"net/http"
"github.com/gorilla/mux"
)
func main() {
router := mux.NewRouter()
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl, _ := template.ParseFiles("index.html")
tmpl.Execute(w, nil)
})
http.Handle("/", router)
http.ListenAndServe(":8181", nil)
}

What did you expect to see?

blabla bla bla bla bla bla bla bla bla bla bla

What did you see instead?

I see raw html code:
....

<title>Title</title>

blabla bla bla bla bla bla bla bla bla bla bla

@SergeyVlasov
Copy link
Author

I solved problem:
w.Header().Set("Content-Type", "text/html") before execute tmpl

@dmitshur
Copy link
Contributor

Setting the Content-Type header explicitly makes it so that browsers (and the HTTP server) don't have to guess what the content type is. Guessing is hard when the content is very short. Glad you figured this out.

Closing since this isn't a bug in the Go project.

@golang golang locked and limited conversation to collaborators Feb 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants