Node-fetch: HTTP and HTTPS agent

Created on 11 Mar 2019  路  3Comments  路  Source: node-fetch/node-fetch

I am trying to use keep-alive agent with node-fetch as described in #423, but I have a problem with HTTPS redirects. Initially, I call a HTTP resource which returns a redirect to a HTTPS resource. In the default node-fetch this is not a problem, because the agent is set based on the current request. When overriding the agent there is no way of having per-request agents.

I would propose an API that allows a function instead of a fixed agent:

fetch(url, { agent: (protocol) => protocol === 'https' ? httpsAgent : httpAgent })
Node-specific enhancement

Most helpful comment

this issue is addressed in v2.6.0, options.agent now accepts a function, see readme:

https://github.com/bitinn/node-fetch#custom-agent

All 3 comments

Yes this is a known limit, but since agent is non-standard I doubt there will be much change to this.

However, if you are lucky enough to be using nodejs 11.7.0+, globalAgent is now replaceable, meaning you can control this precisely:

Thanks for pointing to that change in node 11.

this issue is addressed in v2.6.0, options.agent now accepts a function, see readme:

https://github.com/bitinn/node-fetch#custom-agent

Was this page helpful?
0 / 5 - 0 ratings