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条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog