Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket hang up when using axios.get, but not when using https.get

To the best of my knowledge, I am doing the same thing using 2 different approaches:

const https = require("https");
const axios = require("axios");

let httpsAgent = new https.Agent({rejectUnauthorized: false});

axios.get(`https://${hostname}:${port}${path}`, {httpsAgent})
    .then((data) => { console.log("axios success: " + data.substr(0, 100)); })
    .catch((error) => { console.log("axios error: " + error); });

let data = "";
https.get({ hostname, path, port, agent: httpsAgent },
    (response) => {
        response.on("data", (chunk) => { data += chunk; });
        response.on("end", () => { console.log("https success: " + data.substr(0, 100)); });
    })
    .on("error", (error) => { console.log("https error: " + error); });

When I run this code, i get 2 different outcomes:

PS C:\Users\me> .\node\node.exe .\generate-test-data.js
axios error: Error: socket hang up
https success: [{"cool":"data"...

What is going on here? I have a feeling it has to do with asynchronicity, but not quite sure how... Can somebody give me a hint as to how/why these 2 behaviors are different?

like image 227
Lucas Avatar asked Apr 16 '17 03:04

Lucas


People also ask

What causes a socket hang up?

The error code [socket hang up][ECONNRESET] indicates that the target server has closed the connection with Edge Microgateway. This can be searched in the logs to determine how often it is happening.

Does Axios work with HTTP?

Axios works by making HTTP requests with NodeJS and XMLHttpRequests on the browser. If the request was successful, you will receive a response with the data requested. If the request failed, you will get an error. You can also intercept the requests and responses and transform or modify them.

How do I make HTTP requests with Axios?

A GET request can be made with Axios to “get” data from a server. The HTTP get request is performed by calling axios. get() . The get() method requires two parameters to be supplied to it.

How do I handle Axios POST request?

To perform an HTTP POST request in Axios, call axios. post() . Making a POST request in Axios requires two parameters: the URI of the service endpoint and an object that contains the properties you wish to send to the server. For a simple Axios POST request, the object must have a url property.


1 Answers

ARGH!

After digging around in the axios source, I found this:

if (!proxy) {
  var proxyEnv = protocol.slice(0, -1) + '_proxy';
  var proxyUrl = process.env[proxyEnv] || process.env[proxyEnv.toUpperCase()];
  if (proxyUrl) {
    var parsedProxyUrl = url.parse(proxyUrl);
    proxy = {
      host: parsedProxyUrl.hostname,
      port: parsedProxyUrl.port
    };

    if (parsedProxyUrl.auth) {
      var proxyUrlAuth = parsedProxyUrl.auth.split(':');
      proxy.auth = {
        username: proxyUrlAuth[0],
        password: proxyUrlAuth[1]
      };
    }
  }
}

But nothing for no_proxy. Seems there is a feature request for this... In the meantime, i will just have to:

    delete process.env['http_proxy'];
    delete process.env['HTTP_PROXY'];
    delete process.env['https_proxy'];
    delete process.env['HTTPS_PROXY'];
like image 62
Lucas Avatar answered Sep 20 '22 20:09

Lucas