Standard library / net/http/httputil
httputil.ProxyRequest.SetURL()
Rewrite outbound scheme, host, base path and query using a fixed backend URL. Unlike the older helper, the outbound Host header defaults to the target host. Do not accept arbitrary destination URLs from untrusted users. Expected stdout: 'http://backend.invalid/base/a?q=1 true\n'.
Formal Syntax & Structure
go syntaxgo
func (r *ProxyRequest) SetURL(target *url.URL)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| target | *url.URL | Fixed backend URL including scheme, host and optional base path. |
Working Example
Example Snippetgo
package main
import ( "fmt" "net/http/httptest" "net/http/httputil" "net/url")
func main() { in := httptest.NewRequest("GET", "http://demo.invalid/a?q=1", nil) p := httputil.ProxyRequest{In: in, Out: in.Clone(in.Context())} p.SetURL(must(url.Parse("http://backend.invalid/base"))) fmt.Println(p.Out.URL.String(), p.Out.Host == "")}
func must[T any](v T, e error) T { if e != nil { panic(e) } return v}func check(e error) { if e != nil { panic(e) }}