You need to copy the desired values ββinto a new query. Since this is very similar to what the reverse proxy does, you can see that "net / http / httputil" is for ReverseProxy .
Create a new request and copy only those parts of the request that you want to send to the next server. You will also need to read and fix the request body if you intend to use it in both places:
func handler(w http.ResponseWriter, req *http.Request) { // we need to buffer the body if we want to read it here and send it // in the request. body, err := ioutil.ReadAll(req.Body) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } // you can reassign the body if you need to parse it as multipart req.Body = ioutil.NopCloser(bytes.NewReader(body)) // create a new url from the raw RequestURI sent by the client url := fmt.Sprintf("%s://%s%s", proxyScheme, proxyHost, req.RequestURI) proxyReq, err := http.NewRequest(req.Method, url, bytes.NewReader(body)) // We may want to filter some headers, otherwise we could just use a shallow copy // proxyReq.Header = req.Header proxyReq.Header = make(http.Header) for h, val := range req.Header { proxyReq.Header[h] = val } resp, err := httpClient.Do(proxyReq) if err != nil { http.Error(w, err.Error(), http.StatusBadGateway) return } defer resp.Body.Close() // legacy code }
source share