Search code examples
gogo-gin

How to set new request body in Go Gin while proxing a request


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.


Solution

  • You're probably having a Content-Length mismatch. After the replace, the new body is longer than the previous one.

    Write the Director function as follows:

        proxy.Director = func(req *http.Request) {
            // clone the headers
            req.Header = c.Request.Header.Clone()
    
            // 1. set new header
            req.Header.Set("Content-Length", strconv.Itoa(len(newBody)))
    
            // 2. also update this field
            req.ContentLength = int64(len(newBody))
    
            // the rest stays the same
            req.Host = remote.Host
            req.URL.Scheme = remote.Scheme
            req.URL.Host = remote.Host
            req.URL.Path = remote.Path
        }