Golang ServeHTTP make proxy error EOF on POST request

Issue

I’m working on proxy server with gin and ServeHTTP. 
Actually GET and OPTIONS request works well. But when I trying multiples POST request I get EOF error one in two request.
I’ve test to make repeat request without proxy service and its work well, so there is something not working in my code.

Edit :

  1. I have test POST proxy with https://ptsv2.com/ and all request response return 200 status code.
// main.go

package main

import (
    "fmt"
    "../pkg/api"
)

func main() {
    fmt.Println("Starting server")
    api.InitServer()
}

// routes.go
package api

import (
    "github.com/gin-gonic/gin"
)

const serviceUrl = "http://localhost:8732"

func InitServer() {
    router := gin.Default()
    defineRoutes(router)
    router.Run()
}

func defineRoutes(router *gin.Engine) {
    router.GET("/ping", Ping)
    router.POST("/*any", Proxy(serviceUrl))
}
// controller.go

package api

import (
    "bytes"
    "fmt"
    "io/ioutil"
    "net/http"
    "net/http/httputil"
    "net/url"
    "strconv"
    "github.com/gin-gonic/gin"
)

type transport struct {
    http.RoundTripper
}

func (t *transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
    resp, err = t.RoundTripper.RoundTrip(req)
    if err != nil {
        // EOF ERROR HERE
        return nil, err
    }
    b, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return nil, err
    }
    err = resp.Body.Close()
    if err != nil {
        return nil, err
    }
    b = bytes.Replace(b, []byte("server"), []byte("schmerver"), -1)
    body := ioutil.NopCloser(bytes.NewReader(b))

    response := resp
    response.Body = body


    response.ContentLength = int64(len(b))
    response.Header.Set("Content-Length", strconv.Itoa(len(b)))
    response.Header.Set("Access-Control-Allow-Origin", "*")

    return response, nil
}

func Proxy(targetUrl string) gin.HandlerFunc {
    fn := func(c *gin.Context) {
        remote, err := url.Parse(targetUrl)
        if err != nil {
            panic(err)
        }
        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.Close = true // True / False same result
        }
        proxy.Transport = &transport{http.DefaultTransport}
        proxy.ServeHTTP(c.Writer, c.Request)
    }
    return fn
}

Solution

conclusion

your code have no bug. it works. maybe your network setting is wrong.

explain

I download your code and test it with a local backend server. It works.

enter image description here

enter image description here

appendix

backend server code

package main

import (
    "fmt"

    "github.com/gin-gonic/gin"
)

func main() {
    fmt.Println("Starting server")
    router := gin.Default()
    router.POST("/*any", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "mes": "hello",
        })
    })
    router.Run(":8888")
}

Answered By – Frank Wang

Answer Checked By – Robin (GoLangFix Admin)

Leave a Reply

Your email address will not be published.