Standard library / net/http/httputil

httputil.ProxyRequest

A rewrite callback receives In (read-only incoming request) and Out (modifiable outbound request). Do not retain either after callback return. SetURL and SetXForwarded configure routing and forwarding metadata. Expected stdout: 'true yes\n'.

Formal Syntax & Structure

go syntaxgo
type ProxyRequest struct {  In *http.Request
  Out *http.Request}

Working Example

Example Snippetgo
package main
import (  "fmt"  "net/http/httptest"  "net/http/httputil")
func main() {  in := httptest.NewRequest("GET", "http://demo.invalid/a", nil)  p := httputil.ProxyRequest{In: in, Out: in.Clone(in.Context())}  p.Out.Header.Set("X-Demo", "yes")  fmt.Println(p.In.Header.Get("X-Demo") == "", p.Out.Header.Get("X-Demo"))}
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)  }}
Go Reference