Standard library / net/http

http.ProxyFromEnvironment()

Select a proxy using environment settings; localhost and loopback bypass the proxy. Isolated fixture sets variables before first use; no proxy connection is made. Expected stdout: 'proxy.invalid:8080\n'.

Formal Syntax & Structure

go syntaxgo
func ProxyFromEnvironment(req *Request) (*url.URL, error)

Parameters & Attributes

NameTypeDescription
req*RequestHTTP request associated with the operation.

Return Value

(*url.URL, error)

Operation result; check errors and validity indicators when returned.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net/http"  "os")
func main() {  check(os.Setenv("HTTP_PROXY", "http://proxy.invalid:8080"))  check(os.Setenv("NO_PROXY", ""))  check(os.Unsetenv("REQUEST_METHOD"))  q := must(http.NewRequest("GET", "http://example.invalid", nil))  u := must(http.ProxyFromEnvironment(q))  fmt.Println(u.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)  }}
Go Reference