req icon indicating copy to clipboard operation
req copied to clipboard

How do I use a different proxy for each request

Open shiqiangz-git opened this issue 1 year ago • 2 comments

How do I use a different proxy for each request

shiqiangz-git avatar Sep 23 '24 05:09 shiqiangz-git

How do I use a different proxy for each request

I think it's not possible with goroutines. So I just created a channel of req.Client and use it in the code like this

cl := <-clients
defer func() {clients <- cl}()
cl.SetProxyUrl(...)

*other code*

Deferede avatar Oct 18 '24 09:10 Deferede

Proxy is a client-level only setting, but you can use some hook (e.g. OnBeforeRequest) to change proxy for each request.

The exampoe here may inspire you: https://req.cool/docs/examples/easily-write-crawlers-with-req/#automatically-change-ip-when-request-fails-set-proxy

imroc avatar Oct 18 '24 13:10 imroc