doucai9270 2015-05-20 03:41
浏览 134
已采纳

为什么我的GoLang POST请求没有填充数据?

func forwarderHandlerFunc(w http.ResponseWriter, r *http.Request) {
    client := &http.Client{}
    u, _ := url.Parse(r.RequestURI)
    req, _ := http.NewRequest(r.Method, fmt.Sprintf("%s%s", apiUrl, u.Path), r.Body)
    fmt.Printf(fmt.Sprintf("%s
", nutils.ReaderToString(req.Body)))
    resp, _ := client.Do(req)
    resp.Write(w)
}

I am trying to forward an incoming HTTP request to another endpoint, while copying the body, including POST/PUT form data into the new request.

However, it doesn't seem to work, even if the Body seems to print out correct with data.

Print output is:

email=meh%!g(MISSING)mail.com

How can I fix it?


Edit: Added more debug info, this time, printing out the output of resp

func forwarderHandlerFunc(w http.ResponseWriter, r *http.Request) {
    client := &http.Client{}
    u, _ := url.Parse(r.RequestURI)

    req, _ := http.NewRequest(r.Method, fmt.Sprintf("%s%s", apiUrl, u.Path), r.Body)
    fmt.Printf(fmt.Sprintf("%s
", nutils.ReaderToString(req.Body)))

    resp, _ := client.Do(req)
    b,_ := ioutil.ReadAll(resp.Body)
    fmt.Printf(fmt.Sprintf("%s
", nutils.BytesToString(b)))

    resp.Write(w)
}
$ go install && gom-proxy-forwarder run --listen localhost:5002 --api-url http://localhost:5001
email=meh2%!g(MISSING)mail.com
{
  "email": null
}

It should not be null. It should be meh@gmail.com

  • 写回答

3条回答 默认 最新

  • douliang2087 2015-05-20 04:36
    关注

    Got it. The problem was my endpoint in Python's Flask server does not support chunked encoding, which Go's Request insists on.

    When I manually specified the ContentLength like req.ContentLength = 25, it worked.

    Lesson learnt: It might not always be your Go code be the problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写