It's the simple form from astaxie's book when I try '/login' , I get
No Data received { Unable to load the webpage because the server sent no data. Error code: ERR_EMPTY_RESPONSE }
Here's the code:
main.go
package main
import (
"fmt"
"html/template"
"net/http"
)
func sayhelloName(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello astaxie!") // write data to response
}
func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method) //get request method
if r.Method == "GET" {
t, err :=template.New("").Parse(loginHtml)
if err != nil {
panic(err)
}
const loginHtml = `
<html>
<head>
<title></title>
</head>
<body>
<form action="/login" method="post">
Username:<input type="text" name="username">
Password:<input type="password" name="password">
<input type="submit" value="Login">
</form>
</body>
</html>
`
} else {
r.ParseForm()
// logic part of log in
fmt.Println("username:", r.PostFormValue("username"))
fmt.Println("password:", r.PostFormValue("password"))
}
}
func main() {
http.HandleFunc("/", sayhelloName) // setting router rule
http.HandleFunc("/login", login)
http.ListenAndServe(":9090", nil) // setting listening port
}
#login.gtpl
<html>
<head>
<title></title>
</head>
<body>
<form action="/login" method="post">
Username:<input type="text" name="username">
Password:<input type="password" name="password">
<input type="submit" value="Login">
</form>
</body>
</html>
Any idea??
The problem with your original question (which has been edited several times) was that your ParseFiles()
function failed, it was not able to read your template file. You didn't know about this because the error
it returned you just discarded. Never do that! The least you can do is print the error or call panic(err)
if it occurs. Would you have done that, you would have seen the cause immediately.
The login.gtpl
file has to be placed in the working directory you start you app from if you specify a relative path. Or specify an absolute path.
You can also put your HTML source into your Go file like this until you sort things out:
t, err := template.New("").Parse(loginHtml)
if err != nil {
panic(err)
}
t.Execute(w, nil)
// ... the rest of your code
// At the end of your .go source file (outside of login() func) insert:
const loginHtml = `
<html>
<head>
<title></title>
</head>
<body>
<form action="/login" method="post">
Username:<input type="text" name="username">
Password:<input type="password" name="password">
<input type="submit" value="Login">
</form>
</body>
</html>
`
Note #1:
Since your HTML template is just a static HTML, in its current form you can simply just send it to the output without building and executing a template from it:
// Instead of calling template.New(...).Parse(...) and t.Execute(...), just do:
w.Write([]byte(loginHtml))
Note #2:
The Request.Form
is only available after Request.ParseForm()
has been called, so do that prior to accessing it. Also for POST
forms you might want to use Request.PostForm
instead.
As an alternative you can use the Request.PostFormValue()
method which does this for you automatically if it has not yet been called:
fmt.Println("username:", r.PostFormValue("username"))
fmt.Println("password:", r.PostFormValue("password"))