英文:
r.From["username"] giving empty values
问题
我已经使用golang编写了一个小型的Web应用程序。
> go version go1.7.4 linux/amd64
Go代码:
r.ParseForm()
// 登录逻辑部分
fmt.Println("用户名:", r.Form["username"])
fmt.Println("密码:", r.Form["password"])
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
HTML模板
<body>
<div class="container">
<h1>登录</h1>
<form action="/login" method="POST">
<div class="form-group">
<label for="username">用户名</label>
<input type="text" class="form-control" name="username" placeholder="请输入用户名">
</div>
<div class="form-group">
<label for="password">密码</label>
<input type="password" class="form-control" name="password" placeholder="请输入密码">
</div>
<button type="submit" class="btn btn-success">登录</button>
</form>
</div>
</body>
<!-- end snippet -->
当我使用以下代码打印时:
requestbody, _ := ioutil.ReadAll(r.Body)
log.Println("请求体是", string(requestbody))
它会打印出正确的值,包括输入的用户名和密码。
但是当我尝试使用r.Form
打印时,它会给出空值。
这种方法有什么问题吗?
func loginHandler(w http.ResponseWriter, r *http.Request) {
log.Println("loginHandler")
log.Println("请求URL是", r.RequestURI)
log.Println("请求方法是", r.Method)
requestbody, _ := ioutil.ReadAll(r.Body)
log.Println("请求体是", string(requestbody))
if r.Method == "POST" {
r.ParseForm()
// 登录逻辑部分
fmt.Println("用户名:", r.Form["username"])
fmt.Println("密码:", r.Form["password"])
us, err := globalSessions.SessionStart(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Location", "/auth")
w.WriteHeader(http.StatusFound)
return
}
outputHTML(w, r, "static/login.html")
}
英文:
I have written an small web app using golang
> go version go1.7.4 linux/amd64
Go Code:
r.ParseForm()
// logic part of log in
fmt.Println("username:", r.Form["username"])
fmt.Println("password:", r.Form["password"])
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
HTML template
<body>
<div class="container">
<h1>Login In</h1>
<form action="/login" method="POST">
<div class="form-group">
<label for="username">User Name</label>
<input type="text" class="form-control" name="username" placeholder="Please enter your user name">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" name="password" placeholder="Please enter your password">
</div>
<button type="submit" class="btn btn-success">Login</button>
</form>
</div>
</body>
<!-- end snippet -->
When I am printing using
requestbody, _ := ioutil.ReadAll(r.Body)
log.Println("request body is", string(requestbody))
it's printing correct values along with entered username and password.
But when I tried to print it using r.Form
it's giving empty value.
Is anything wrong in this approach?
func loginHandler(w http.ResponseWriter, r *http.Request) {
log.Println("loginHandler")
log.Println("request url is", r.RequestURI)
log.Println("request method", r.Method)
requestbody, _ := ioutil.ReadAll(r.Body)
log.Println("request body is", string(requestbody))
if r.Method == "POST" {
r.ParseForm()
// logic part of log in
fmt.Println("username:", r.Form["username"])
fmt.Println("password:", r.Form["password"])
us, err := globalSessions.SessionStart(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Location", "/auth")
w.WriteHeader(http.StatusFound)
return
}
outputHTML(w, r, "static/login.html")
}
答案1
得分: 3
请将下面的代码进行注释:
requestbody, _ := ioutil.ReadAll(r.Body)
因为Request.Body
的类型是ReadCloser
,这会导致r.Body
被清空。
参考资料:Golang读取请求体
英文:
Comment the code below:
requestbody, _ := ioutil.ReadAll(r.Body)
Because Request.Body
's type is ReadCloser
that will empty r.Body
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论