Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxy route to another backend #686

Closed
kidambisrinivas opened this issue Aug 16, 2016 · 8 comments
Closed

Proxy route to another backend #686

kidambisrinivas opened this issue Aug 16, 2016 · 8 comments

Comments

@kidambisrinivas
Copy link

How to reverse proxy web requests for a few routes to another backend in Gin Gonic web golang framework

Is there a way to directly forward in the Handle function as shown below?

router := gin.New()
router.Handle("POST", "/api/v1/endpoint1", ForwardToAnotherBackend)
@kidambisrinivas kidambisrinivas changed the title Golang gin gonic web framework proxy route to another backend Proxy route to another backend Aug 16, 2016
@stxml
Copy link

stxml commented Aug 16, 2016

Maybe use a middleware to set up a ReverseProxy?

router.POST("/api/v1/endpoint1", ReverseProxy("localhost:8080/otherBackend")

func ReverseProxy(target string) gin.HandlerFunc {
    url, err := url.Parse(target)
    checkErr(err)
    proxy := httputil.NewSingleHostReverseProxy(url)
    return func(c *gin.Context) {
        proxy.ServeHTTP(c.Writer, c.Request)
    }
}

@kidambisrinivas
Copy link
Author

Thanks, this worked. But the headers are not being passed to the backend

@kidambisrinivas
Copy link
Author

kidambisrinivas commented Aug 18, 2016

Got this sorted out with this:

router.POST("/api/v1/endpoint1", ReverseProxy()

func ReverseProxy() gin.HandlerFunc {

    target := "localhost:3000"

    return func(c *gin.Context) {
        director := func(req *http.Request) {
            r := c.Request
            req = r
            req.URL.Scheme = "http"
            req.URL.Host = target
            req.Header["my-header"] = []string{r.Header.Get("my-header")}
                        // Golang camelcases headers
            delete(req.Header, "My-Header")
        }
        proxy := &httputil.ReverseProxy{Director: director}
        proxy.ServeHTTP(c.Writer, c.Request)
    }
}

@stxml
Copy link

stxml commented Aug 18, 2016

Weird. I whipped up a test and I only had to add http:// to the string to make it work. It also correctly passed on my header. Maybe your header is one of the special cases that gets filtered out.

@zwhitchcox
Copy link

Does this pass cookies?

@zwhitchcox
Copy link

Oh sorry, cookies are in the header

@zheeeng
Copy link

zheeeng commented Jan 18, 2019

Can we have a chance to manipulate the response?

@sguillia
Copy link

@kidambisrinivas you should remove both lines to make it work:

r := c.Request
req = r

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants