dongqing8765 2017-02-02 21:46
浏览 207
已采纳

r.PostForm和r.Form始终为空

I have a very strange problem, and i'm either really blind, or this is some kind of a bug. I have the following http.Handler:

func ServeHTTP(w http.ResponseWriter, r *http.Request) {
    err := r.ParseForm()
    if err != nil {
        log.Println("Error while parsing form data")
        return
    }
    log.Println("Printing r.PostForm:")
    for key, values := range r.PostForm { // range over map
        for _, value := range values { // range over []string
            log.Println(key, value)
        }
    }
    b, _ := ioutil.ReadAll(r.Body)
    s := string(b)
    log.Println("Printing body: ",s)
}

Now, when sending a PUT-Request to the url binded to this handler with the following FORM-Data:

Name=someName
Version=1.0.0
PLanguage=java
GitRepo=someRepo

This is ALWAYS the output:

Printing r.PostForm:
Printing body: Name=someName&Version=1.0.0&PLanguage=java&GitRepo=someRepo

I've been trying to find the cause for like 2 hours already and i just have no idea what the heck is wrong here. There is no error parsing the Form-Data, but the r.PostForm map is always empty (i also tried r.Form, with same result). So for debugging i added the part where i print the body, just to make sure there actually is some data in there - and it is. I would really appreciate any help here. Thanks in advance!

  • 写回答

1条回答 默认 最新

  • doukui2011 2017-02-02 22:13
    关注

    You need to set the 'Content-Type' header.

    If no header is set "application/octet-stream" is used according to RFC 2616.

    Long story short that is a binary format so your body will not be parsed into the Form.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线