zoukankan      html  css  js  c++  java
  • Go语言http包Form解析之坑

    最近正在用Go语言做一个项目,今天在用http包读取客户端发过来的POST数据时遇到了一点小麻烦,就下面这段代码,死活读不到数据:

    if r.ContentLength > 0 {
        var body []byte
        nRead, err := r.Body.Read(body)   
        if err != nil {
            fmt.Println(err.Error())    
        }
    }

    返回的错误值是EOF。通过检查header信息,发现content-type是application/x-www-form-urlencoded。检查Request信息也发现ContentLength的值大于0。仔细过了一遍前面的代码,发现在刚开始处理请求的地方,为了输出http.Request的所有信息,调用下面这个函数来解析Form数据:

    r.ParseForm()

    试着把这句注释掉,可以读到数据了!

    查看了ParseForm的源代码,发现它已经读取了Request Body里的数据:

        if r.Method == "POST" || r.Method == "PUT" {
        if r.Body == nil {
            return errors.New("missing form body")
            }
        ct := r.Header.Get("Content-Type")
        ct, _, err = mime.ParseMediaType(ct)
        switch {
        case ct == "application/x-www-form-urlencoded":
        var reader io.Reader = r.Body
            maxFormSize := int64(1<<63 - 1)
        if _, ok := r.Body.(*maxBytesReader); !ok {
            maxFormSize = int64(10 << 20) // 10 MB is a lot of text.
            reader = io.LimitReader(r.Body, maxFormSize+1)
        }
        b, e := ioutil.ReadAll(reader)
        if e != nil {
            if err == nil {
            err = e
            }
            break
        }
            

    同时还要注意的有以下几个函数:

    func (r *Request) ParseMultipartForm(maxMemory int64) error
    func (r *Request) FormValue(key string) string
    func (r *Request) FormFile(key string)

    它们可能会直接或间接的调用ParseForm,同样会造成Body数据被读取。虽说浪费了一些时间,但好在及时跳出了这个坑,也希望这个教训能给大家一点帮助。

  • 相关阅读:
    Servlet项目 创建方法
    1. 连接数据库
    Jsp application对象(全局变量)
    Jsp session属性、方法
    让python 3支持mysqldb的解决方法
    Python读写文件(进阶)
    python操作MongoDB
    使用Python Pandas处理亿级数据
    SurfingTheInternet
    matplotlib中日期显示(不显示为科学计数法)
  • 原文地址:https://www.cnblogs.com/technic-emotion/p/3655404.html
Copyright © 2011-2022 走看看