donglu6303
2017-10-30 18:25
浏览 83
已采纳

进行HTTP表单解析-返回空的slice / empty值?

I have written a simple web application in Go that needs to read values of an HTTP form (username, password) etc. However, I found that the values were empty when printed. len(r.Form) and len(r.Form["password"]) both return 0.

I have called r.ParseForm() in the application before trying to read the fields and I'm using Postman to send the requests. Tested on both Linux and macOS.

The code I am using to test this is some example code from the Astaxie golang web tutorial. I have attached my Postman request. It looks like this so far:

package main

import (
    "fmt"
    "html/template"
    "log"
    "net/http"
    "strings"
    "time"
)

func sayhelloName(w http.ResponseWriter, r *http.Request) {
    r.ParseForm() //Parse url parameters passed, then parse the response packet for the POST body (request body)
    // attention: If you do not call ParseForm method, the following data can not be obtained form
    fmt.Println(r.Form) // print information on server side.
    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!") // 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, _ := template.ParseFiles("login.gtpl")
        t.Execute(w, nil)
    } else {
        r.ParseForm()
        time.Sleep(3 * time.Second)
        // logic part of log in
        fmt.Println("username:", len(r.Form))
        fmt.Println("password:", len(r.Form["password"]))
    }

}

func main() {
    http.HandleFunc("/", sayhelloName) // setting router rule
    http.HandleFunc("/login", login)
    err := http.ListenAndServe(":9090", nil) // setting listening port
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

Any suggestions on what to do next?

Thanks!

图片转代码服务由CSDN问答提供 功能建议

我在Go中编写了一个简单的Web应用程序,该应用程序需要读取HTTP形式的值(用户名,密码)等 但是,我发现打印时这些值是空的。 len(r.Form) len(r.Form [“ password”])都返回0。

我已经打电话给我 在尝试读取字段之前,在应用程序中使用 r.ParseForm(),并且我正在使用Postman发送请求。 在Linux和macOS上都经过测试。

我用来测试的代码是Astaxie golang网络教程中的一些示例代码。 我已附上邮递员请求。 到目前为止看起来像这样:

 包main 
 
import(
“ fmt” 
“ html / template” 
“ log” 
“ net /  http“ 
”字符串“ 
”时间“ 
)
 
func sayhelloName(w http.ResponseWriter,r * http.Request){
 r.ParseForm()//解析传递的url参数,然后解析 POST正文(请求正文)的响应数据包
 //注意:如果不调用ParseForm方法,则无法从表单获取以下数据
 fmt.Println(r.Form)//在服务器端打印信息。  
 fmt.Println(“ path”,r.URL.Path)
 fmt.Println(“ scheme”,r.URL.Scheme)
 fmt.Println(r.Form [“ url_long”])
  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)//获取请求方法
,如果r.Method ==“ GET” {
t,_:= template.ParseFiles(“ login.gtpl”)
  t.Execute(w,nil)
} else {
 r.ParseForm()
 time.Sleep(3 * time.Second)
 //登录的逻辑部分
 fmt.Println(“用户名:  “,len(r.Form))
 fmt.Println(” password:“,len(r.Form [” password“]))
} 
 
} 
 
func main(){
  http.HandleFunc(“ /”,sayhelloName)//设置路由器规则
 http.HandleFunc(“ / login”,登录)
 err:= http.ListenAndServe(“:9090”,nil)//设置监听端口\  n如果err!= nil {
 log.Fatal(“ ListenAndServe:”,err)
} 
} 
   
 
 

关于下一步操作的任何建议 ?

谢谢!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doucheng1884 2017-10-30 18:33
    已采纳

    try changing the content-type on your Postman request from form-data to x-www-form-urlencoded

    Because according to the docs on r.ParseForm() the body won't be parsed unless it's x-www-form-urlencoded

    For other HTTP methods, or when the Content-Type is not application/x-www-form-urlencoded, the request Body is not read, and r.PostForm is initialized to a non-nil, empty value.

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题