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

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

r.From["username"] giving empty values

问题

我已经使用golang编写了一个小型的Web应用程序。

> go version go1.7.4 linux/amd64
Go代码:

  1. r.ParseForm()
  2. // 登录逻辑部分
  3. fmt.Println("用户名:", r.Form["username"])
  4. fmt.Println("密码:", r.Form["password"])

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

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

  1. HTML模板
  2. &lt;body&gt;
  3. &lt;div class="container"&gt;
  4. &lt;h1&gt;登录&lt;/h1&gt;
  5. &lt;form action="/login" method="POST"&gt;
  6. &lt;div class="form-group"&gt;
  7. &lt;label for="username"&gt;用户名&lt;/label&gt;
  8. &lt;input type="text" class="form-control" name="username" placeholder="请输入用户名"&gt;
  9. &lt;/div&gt;
  10. &lt;div class="form-group"&gt;
  11. &lt;label for="password"&gt;密码&lt;/label&gt;
  12. &lt;input type="password" class="form-control" name="password" placeholder="请输入密码"&gt;
  13. &lt;/div&gt;
  14. &lt;button type="submit" class="btn btn-success"&gt;登录&lt;/button&gt;
  15. &lt;/form&gt;
  16. &lt;/div&gt;
  17. &lt;/body&gt;

<!-- end snippet -->

当我使用以下代码打印时:

  1. requestbody, _ := ioutil.ReadAll(r.Body)
  2. log.Println("请求体是", string(requestbody))

它会打印出正确的值,包括输入的用户名和密码。
但是当我尝试使用r.Form打印时,它会给出空值。
这种方法有什么问题吗?

  1. func loginHandler(w http.ResponseWriter, r *http.Request) {
  2. log.Println("loginHandler")
  3. log.Println("请求URL是", r.RequestURI)
  4. log.Println("请求方法是", r.Method)
  5. requestbody, _ := ioutil.ReadAll(r.Body)
  6. log.Println("请求体是", string(requestbody))
  7. if r.Method == "POST" {
  8. r.ParseForm()
  9. // 登录逻辑部分
  10. fmt.Println("用户名:", r.Form["username"])
  11. fmt.Println("密码:", r.Form["password"])
  12. us, err := globalSessions.SessionStart(w, r)
  13. if err != nil {
  14. http.Error(w, err.Error(), http.StatusInternalServerError)
  15. return
  16. }
  17. w.Header().Set("Location", "/auth")
  18. w.WriteHeader(http.StatusFound)
  19. return
  20. }
  21. outputHTML(w, r, "static/login.html")
  22. }
英文:

I have written an small web app using golang

> go version go1.7.4 linux/amd64
Go Code:

  1. r.ParseForm()
  2. // logic part of log in
  3. fmt.Println(&quot;username:&quot;, r.Form[&quot;username&quot;])
  4. fmt.Println(&quot;password:&quot;, r.Form[&quot;password&quot;])

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

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

  1. HTML template
  2. &lt;body&gt;
  3. &lt;div class=&quot;container&quot;&gt;
  4. &lt;h1&gt;Login In&lt;/h1&gt;
  5. &lt;form action=&quot;/login&quot; method=&quot;POST&quot;&gt;
  6. &lt;div class=&quot;form-group&quot;&gt;
  7. &lt;label for=&quot;username&quot;&gt;User Name&lt;/label&gt;
  8. &lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;username&quot; placeholder=&quot;Please enter your user name&quot;&gt;
  9. &lt;/div&gt;
  10. &lt;div class=&quot;form-group&quot;&gt;
  11. &lt;label for=&quot;password&quot;&gt;Password&lt;/label&gt;
  12. &lt;input type=&quot;password&quot; class=&quot;form-control&quot; name=&quot;password&quot; placeholder=&quot;Please enter your password&quot;&gt;
  13. &lt;/div&gt;
  14. &lt;button type=&quot;submit&quot; class=&quot;btn btn-success&quot;&gt;Login&lt;/button&gt;
  15. &lt;/form&gt;
  16. &lt;/div&gt;
  17. &lt;/body&gt;

<!-- end snippet -->

When I am printing using

  1. requestbody, _ := ioutil.ReadAll(r.Body)
  2. 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?

  1. func loginHandler(w http.ResponseWriter, r *http.Request) {
  2. log.Println(&quot;loginHandler&quot;)
  3. log.Println(&quot;request url is&quot;, r.RequestURI)
  4. log.Println(&quot;request method&quot;, r.Method)
  5. requestbody, _ := ioutil.ReadAll(r.Body)
  6. log.Println(&quot;request body is&quot;, string(requestbody))
  7. if r.Method == &quot;POST&quot; {
  8. r.ParseForm()
  9. // logic part of log in
  10. fmt.Println(&quot;username:&quot;, r.Form[&quot;username&quot;])
  11. fmt.Println(&quot;password:&quot;, r.Form[&quot;password&quot;])
  12. us, err := globalSessions.SessionStart(w, r)
  13. if err != nil {
  14. http.Error(w, err.Error(), http.StatusInternalServerError)
  15. return
  16. }
  17. w.Header().Set(&quot;Location&quot;, &quot;/auth&quot;)
  18. w.WriteHeader(http.StatusFound)
  19. return
  20. }
  21. outputHTML(w, r, &quot;static/login.html&quot;)
  22. }

答案1

得分: 3

请将下面的代码进行注释:

  1. requestbody, _ := ioutil.ReadAll(r.Body)

因为Request.Body的类型是ReadCloser,这会导致r.Body被清空。

参考资料:Golang读取请求体

英文:

Comment the code below:

  1. 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:

确定