-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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 middleware: add ModifyRequest function to modify http.Request before proxy #2043
Conversation
…es to http.Request before passing it to underlysing ReverseProxy
@@ -1,4 +1,4 @@ | |||
module github.com/labstack/echo/v4 | |||
module github.com/binhnguyenduc/echo/v4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this has to be removed
@@ -54,6 +54,9 @@ type ( | |||
// Examples: If custom TLS certificates are required. | |||
Transport http.RoundTripper | |||
|
|||
// ModifyRequest defines function to modify request before passing to ReverseProxy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Transport
field provides similar functionality as you can create your own http.RoundTripper
which modifies request:
type MyRoundTripper struct {
accessToken string
r http.RoundTripper
}
func (mrt MyRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Add("Authorization", "Bearer: "+mrt.accessToken)
return mrt.r.RoundTrip(r)
}
func main() {
e := echo.New()
e.Use(middleware.ProxyWithConfig(middleware.ProxyConfig{
Transport: MyRoundTripper{},
}))
log.Fatal(e.Start(":8080"))
}
https://stackoverflow.com/questions/51325704/adding-a-default-http-header-in-go
@aldas is it possible to modify the proxy target urls at runtime? |
I think you are looking for: Line 69 in 4a1ccdf
|
@aldas oh so I can use the returned object from The interface is already implemented internally? I just have to call addTarget with the right parameter ? |
The proxy middleware currently does not allow any modification to the request, only ModifyResponse. This PR adds that option so caller can pass a custom function to be applied to requests before it hits reverse proxy.
I have also added passing test.
Please consider merging this as it would allow for more flexibility when proxy with Echo.
Thanks!