r.From[“username”]给出了空值。

huangapple go评论92阅读模式
英文:

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模板
&lt;body&gt;
    &lt;div class="container"&gt;
        &lt;h1&gt;登录&lt;/h1&gt;
        &lt;form action="/login" method="POST"&gt;
            &lt;div class="form-group"&gt;
                &lt;label for="username"&gt;用户名&lt;/label&gt;
                &lt;input type="text" class="form-control" name="username" placeholder="请输入用户名"&gt;
            &lt;/div&gt;
            &lt;div class="form-group"&gt;
                &lt;label for="password"&gt;密码&lt;/label&gt;
                &lt;input type="password" class="form-control" name="password" placeholder="请输入密码"&gt;
            &lt;/div&gt;
            &lt;button type="submit" class="btn btn-success"&gt;登录&lt;/button&gt;
        &lt;/form&gt;
    &lt;/div&gt;
&lt;/body&gt;

<!-- 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(&quot;username:&quot;, r.Form[&quot;username&quot;])
		fmt.Println(&quot;password:&quot;, r.Form[&quot;password&quot;])

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

HTML template
&lt;body&gt;
    &lt;div class=&quot;container&quot;&gt;
        &lt;h1&gt;Login In&lt;/h1&gt;
        &lt;form action=&quot;/login&quot; method=&quot;POST&quot;&gt;
            &lt;div class=&quot;form-group&quot;&gt;
                &lt;label for=&quot;username&quot;&gt;User Name&lt;/label&gt;
                &lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;username&quot; placeholder=&quot;Please enter your user name&quot;&gt;
            &lt;/div&gt;
            &lt;div class=&quot;form-group&quot;&gt;
                &lt;label for=&quot;password&quot;&gt;Password&lt;/label&gt;
                &lt;input type=&quot;password&quot; class=&quot;form-control&quot; name=&quot;password&quot; placeholder=&quot;Please enter your password&quot;&gt;
            &lt;/div&gt;
            &lt;button type=&quot;submit&quot; class=&quot;btn btn-success&quot;&gt;Login&lt;/button&gt;
        &lt;/form&gt;
    &lt;/div&gt;
&lt;/body&gt;

<!-- end snippet -->

When I am printing using

requestbody, _ := ioutil.ReadAll(r.Body)
log.Println(&quot;request body is&quot;, 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(&quot;loginHandler&quot;)
	log.Println(&quot;request url is&quot;, r.RequestURI)
	log.Println(&quot;request method&quot;, r.Method)
	requestbody, _ := ioutil.ReadAll(r.Body)
	log.Println(&quot;request body is&quot;, string(requestbody))
	if r.Method == &quot;POST&quot; {
		r.ParseForm()
		// logic part of log in
		fmt.Println(&quot;username:&quot;, r.Form[&quot;username&quot;])
		fmt.Println(&quot;password:&quot;, r.Form[&quot;password&quot;])
		us, err := globalSessions.SessionStart(w, r)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		w.Header().Set(&quot;Location&quot;, &quot;/auth&quot;)
		w.WriteHeader(http.StatusFound)
		return
	}
	outputHTML(w, r, &quot;static/login.html&quot;)
}

答案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.

FYI, Golang read request body

huangapple
  • 本文由 发表于 2017年4月24日 14:11:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/43580900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定