Search code examples
goreverse-proxygo-gin

Golang gin gonic web framework proxy route to another backend


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)


Solution

  • You can do this with the standard library httputil.ReverseProxy.

    I haven't found a reason to use gin myself yet, I'm a fan of sticking to stdlib whenever possible. However I believe you can wrap this ReverseProxy handler in gin.WrapH() to be able to use it with your gin router.