I'm trying to proxy an HTTP call, and trying to modify the JSON body before sending it to the proxy service. However, if I try to modify the c.Request.Body with any new data, the POST request ends in 400 bad format. But if I set the same previous body data again to c.Request.Body then the proxy call works without a problem.
Gin Function
func Forward(c *gin.Context) {
remoteURL := c.Query("url")
remote, _ := url.Parse(remoteURL)
var bodyBytes []byte
if c.Request.Body != nil {
bodyBytes, _ = ioutil.ReadAll(c.Request.Body)
}
newBody := string(bodyBytes)
newBody = strings.Replace(newBody, "testString", "testString1", -1)
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(newBody)))
proxy := httputil.NewSingleHostReverseProxy(remote)
proxy.Director = func(req *http.Request) {
req.Header = c.Request.Header
req.Host = remote.Host
req.URL.Scheme = remote.Scheme
req.URL.Host = remote.Host
req.URL.Path = remote.Path
}
proxy.ServeHTTP(c.Writer, c.Request)
}
Curl Command:
curl --location --request POST 'http://localhost:8020/v1/proxy?url=https://entgkdbyzqm27.x.pipedream.net/' \
--header 'Content-Type: application/json' \
--data-raw '{
"id": "testString"
}'
I would be much obliged if I could know how to set the request body correctly in Gin.