fetch vs http difference #52894
Unanswered
shanmugharajk
asked this question in
General
Replies: 1 comment
-
I read the doc about proxy-agents(code), did you configured You could also try to use the built-in https.Agent. See the example code from NodeJS: how to route HTTPS requests through an HTTP proxy without any external dependencies (optimized version). The main code is the followings. // Async private method, used only internally by HttpsWithProxyClient
// Handle the connection with the HTTP proxy
// In case the connection is successful, it returns a new http.Agent with keepAlive activated
async _connectToProxy(url) {
return new Promise((resolve, reject) => {
const headers = {
'Proxy-Authorization': 'Basic ' + Buffer.from(this.proxyConf.proxy_username + ':' + this.proxyConf.proxy_password).toString('base64')
}
const urlParsed = new URL(url);
http.request({ // establishing a tunnel
host: this.proxyConf.proxy_host,
port: this.proxyConf.proxy_port,
method: 'CONNECT',
path: `${urlParsed.hostname}:443`,
headers
}).on('connect', (res, socket) => {
if (res.statusCode === 200) {
resolve(new https.Agent({ socket: socket, keepAlive: true }));
} else {
reject('Could not connect to proxy!')
}
}).on('error', (err) => {
reject(err.message);
}).on('timeout', (err) => {
reject(err.message);
}).end();
});
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
If I use the fetch with proxy setup it works fine. In the same machine if I use http it throws with
ECONNRESET
error. I don't know what's the difference between these two core implementations..I got into this issue originally by using the
splunk-logging
library which internally usingneedle
which uses the nativehttp
modules. The I tired with thehttp
module separately and got thisECONNRESET
.Could someone help me understand how it can happen like this between fetch vs http?
fetch example which works
https request code
Beta Was this translation helpful? Give feedback.
All reactions