最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Make a request in native Fetch with Proxy in NodeJS 18 - Stack Overflow

programmeradmin3浏览0评论

In version 18 of Node JS there is already the possibility of making request Fetch without installing packages (example: Axios).

My question is if you can make a request to this Native Fetch with Proxy without installing packages or do you have to install packages to use a Proxy in Fetch?

In case I have to install a package to use Proxy in Fetch, what would be the best one to use with Node's new Fetch?

I really appreciate it if you can leave an implementation code, thanks!

In version 18 of Node JS there is already the possibility of making request Fetch without installing packages (example: Axios).

My question is if you can make a request to this Native Fetch with Proxy without installing packages or do you have to install packages to use a Proxy in Fetch?

In case I have to install a package to use Proxy in Fetch, what would be the best one to use with Node's new Fetch?

I really appreciate it if you can leave an implementation code, thanks!

Share Improve this question asked May 19, 2022 at 14:08 bruxsbruxs 3231 gold badge3 silver badges6 bronze badges 4
  • you can use http_proxy and https_proxy environment variable here too,( i think ) – ben Commented May 19, 2022 at 15:16
  • 1 I have tried many npm proxies and none have worked with native fetch. It is very frustrating to find a working proxy. – bruxs Commented May 25, 2022 at 5:13
  • then you may want to take a look at this HttpAgent implementation and try to implement a light version of it in your code (its using node http lib to implement an proxy agent, no external thing there) if you don't wan't to use any external library. here: https://github.com/TooTallNate/node-http-proxy-agent/blob/master/src/agent.ts. and take a look at the example in the readme so you get some idea how to use it – ben Commented May 25, 2022 at 8:29
  • @ben: Those environment variables are not yet supported, see this issue. – Allon Guralnek Commented Jun 19, 2023 at 3:07
Add a comment  | 

2 Answers 2

Reset to default 11

I got proxies to work with native fetch(), but I did have to install a package (this is because they haven't exposed the functionality to set the proxy nativly, see this issue mentioned in the other answer). This answer might also be useful to those looking to set a proxy server for existing packages that use native fetch() (in my case, Octokit).

I couldn't get it to accept the certificate it presented, as it was signed with an internal Root CA and returned an error Error: self-signed certificate in certificate chain (SELF_SIGNED_CERT_IN_CHAIN). I tried setting the env var NODE_EXTRA_CA_CERTS to a file with the required root CA, to no avail. I also tried specifying {rejectUnauthorized: false} in both the ProxyAgent ctor and the options for the fetch itself, but it didn't have any effect. So I decided to set the env var NODE_TLS_REJECT_UNAUTHORIZED. If anyone knows how to get custom root CAs to work with native fetch() and proxies, please contribute.

After installing the undici NPM package (via npm install undici or yarn add undici), use the following to grab the proxy config from the https_proxy env var:

import { env } from "process";
import { setGlobalDispatcher, ProxyAgent } from "undici";

if (env.https_proxy) {
  // Corporate proxy uses CA not in undici's certificate store
  process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
  const dispatcher = new ProxyAgent({uri: new URL(env.https_proxy).toString() });
  setGlobalDispatcher(dispatcher);
}

await fetch("https://www.stackoverflow.com");

Node 18 does not support proxies with native fetch yet (https://github.com/nodejs/node/issues/42814), but it is being worked on https://github.com/nodejs/node/issues/43187. It does not appear to have been implemented in time for Node 20.

Edit: Now with Node 20 released, this is working for us. Though it does use node's new fetch, it isn't quite what you asked for as it also requires an import of undici to support the proxy. But as undici is also developed by Node maintainers and inspired Node's native fetch implementation, maybe this is close enough.

import { ProxyAgent } from 'undici'
const dispatcher = new ProxyAgent('https://proxy.com')


const r = await fetch('google.com', { 
         dispatcher,
         method: 'POST',
         body: JSON.stringify({ hi: "mom" })
})
发布评论

评论列表(0)

  1. 暂无评论