Proxy Proxy Settings

When the HTTP client initiates a request, it can set the proxy server address proxyURL, which is implemented using SetProxy* related methods. The proxy mainly supports two forms: http and socks5, which are respectively in the form of http://USER:PASSWORD@IP:PORT or socks5://USER:PASSWORD@IP:PORT.

Method list:

  1. func (c *Client) SetProxy(proxyURL string)
  2. func (c *Client) Proxy(proxyURL string) *Client

Let’s look at an example of setting proxyURL with the client.

Normal Call Example

Using the SetProxy configuration method.

  1. client := g.Client()
  2. client.SetProxy("http://127.0.0.1:1081")
  3. client.SetTimeout(5 * time.Second)
  4. response, err := client.Get(gctx.New(), "https://api.ip.sb/ip")
  5. if err != nil {
  6. fmt.Println(err)
  7. }
  8. response.RawDump()

Chained Call Example

Using the Proxy chained method.

  1. client := g.Client()
  2. response, err := client.Proxy("http://127.0.0.1:1081").Get(gctx.New(), "https://api.ip.sb/ip")
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. fmt.Println(response.RawResponse())