いつもお世話になります。postしたデータがfmt.Printlnで取得できないです。その理由が知りたいです。
よろしくお願いします。
login.html
<html> <head> <title></title> </head> <body> <form action="/login" method="post"> ユーザ名:<input type="text" name="username"> パスワード:<input type="password" name="password"> <input type="submit" value="ログイン"> </form> </body> </html>
server.go
package main import ( "fmt" "html/template" "net/http" "strings" ) func sayhelloName(w http.ResponseWriter, r *http.Request) { r.ParseForm() fmt.Println(r.Form) fmt.Println("path", r.URL.Path) fmt.Println("scheme", r.URL.Scheme) fmt.Println(r.Form["url_long"]) for k, v := range r.Form { fmt.Println("key:", k) fmt.Println("val:", strings.Join(v, "")) } fmt.Fprintf(w, "Hello astaxie!") } func login(w http.ResponseWriter, r *http.Request) { fmt.Println("method:", r.Method) if r.Method == "GET" { t, err := template.ParseFiles("login.html") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } t.Execute(w, nil) } else { fmt.Println("username:", r.PostForm["username"]) fmt.Println("password:", r.PostForm["password"]) } } func main() { server := http.Server{ Addr: "127.0.0.1:9000", } http.HandleFunc("/", sayhelloName) http.HandleFunc("/login", login) server.ListenAndServe() }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/16 05:21