resty icon indicating copy to clipboard operation
resty copied to clipboard

What's the idiomatic way of changing proxies for the client?

Open rew1nter opened this issue 8 months ago • 3 comments

I'm currently doing smth like this which I don't think is the best way to change proxies. How's this intended to be done?

package main

import (
	"crypto/tls"
	"fmt"
	"time"

	"github.com/go-resty/resty/v2"
)

var gproxy string = "http://127.0.0.1:8081"

func main() {
	c := resty.New().
		SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}).
		SetRedirectPolicy(resty.FlexibleRedirectPolicy(4))

	// change proxy in a single go routine
	go func() {
		for {
			time.Sleep(2 * time.Second)
			gproxy = "http://127.0.0.1:8080"
		}
	}()

	for {
		time.Sleep(1 * time.Second)
		r, _ := c.
			SetProxy(gproxy).
			R().Get("https://google.com/")
		fmt.Println(r.StatusCode())
	}
}

rew1nter avatar Oct 25 '23 18:10 rew1nter