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\n", 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\n", nutils.ReaderToString(req.Body)))
resp, _ := client.Do(req)
b,_ := ioutil.ReadAll(resp.Body)
fmt.Printf(fmt.Sprintf("%s\n", 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 [email protected]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With