Getting connect ECONNREFUSED 127.0.0.1:80 when attempting HTTP request
Asked Answered
A

12

52

I am attempting to make a http request to news.google.com using the native node.js http module. I am getting the connect ECONNREFUSED 127.0.0.1:80 error when I tried the following

var http = require('http');

var payload = JSON.stringify({
    name: 'John Smith',
    email: '[email protected]',
    resume: 'https://drive.google.com/open?id=asgsaegsehsehseh'
});

var options = {
    hostname: 'https://news.google.com',
    path: '/',
    method: 'GET'
};

var httpRequest = http.request(options, function(request, response) {
    console.log('STATUS', response.statusCode);
    response.setEncoding('utf8');

    response.on('data', function(chunk) {
        console.log('BODY:', chunk);
    });

    response.on('end', function() {
        console.log('No more data in response');
    });
});

httpRequest.on('error', function(e) {
    console.log('Error with the request:', e.message);
});

httpRequest.write(payload);
httpRequest.end();

Why am I getting this error?

I tried using the request npm module. And it worked!

Albur answered 7/10, 2016 at 6:25 Comment(0)
M
102

In my case, issue was actually default behaviour of HTTP client that I was using, axios.

By default, axios redirects us to 127.0.0.1:80 if it doesn't find requested URL or http method(GET/POST/PUT). So better check your URL if are also using axios.

Madame answered 11/2, 2019 at 8:37 Comment(2)
Thanks for your help. inside server of reactjs (ssr.tsx) the Axios url was not correct.Squier
If you're using nextjs, and using the same function to fetch data on client side and server side using relative url, it might not work on the server side. You need to give an absolute path on the server side. On client side, Axios automatically gets window url.Forgiving
P
73

My problem was while using supertest and jest. My mistake was not putting "/" as a prefix to some url. So, double check if the url for the request you are making is proper.

Punctate answered 18/11, 2019 at 16:40 Comment(7)
Had the same experience. Forgot a leading '/' before doing the request with supertest and jest. Dziekuje bardzo.Toile
for some weird reason, this works for me. thank God and stack overflow.Rooky
I missed the leading "/" myself. This helped. ThanksKerin
I had the leading / but mistakenly also had an unnecessary .. Thank you!Forbis
I made the same mistake, This helped me. Thank you!Flitting
Mine was the same. Thanks a lotEngine
this saved me hours of debugging...Paving
L
10

I'm using axios and this error occurred with get request, solved it by adding http:// before my URL (in my case the server is http)

Lowbrow answered 4/5, 2020 at 12:12 Comment(0)
D
6

There are several issues here:

  1. The hostname field of the options structure should be just the host, not a URL. In your case it should just be 'news.google.com'.

  2. The signature for the callback to the request method is function (response) -- yours is function (request, response). Lose the first parameter.

  3. As written this will aways return an HTTP redirection to the https site. Replace var http = require('http'); with var https = require('https'); and then use https everywhere instead of http.

Dentifrice answered 7/10, 2016 at 6:56 Comment(0)
C
3

I encountered this issue while using supertest and jest. I made the mistake of using ./users instead of /users as the url.

Crispate answered 3/4, 2022 at 12:59 Comment(2)
Its common mistake when you give wrong pathRipsaw
Its common issue when we use wrong path in http callsRipsaw
P
3

I had the same problem with jest and supertest. Just changed api/blogs to /api/blogs and it worked !

Plainsong answered 1/9, 2022 at 14:46 Comment(0)
L
0

I had the same problem. Solve it by fixing the HTTP request path mistake in my code.

Louettalough answered 14/9, 2021 at 14:39 Comment(0)
L
0

I had this problem, and my solution (That might not be the same for most people) was that the server was not listening yet, I had to call the axios function after.

Locus answered 16/10, 2021 at 3:54 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Kylynn
S
0

store the root url in some variable or environtment and append it before request so instead of axios.get('/api/myurl') do something like let baseUrl = 'https://google.com"

axios.get(baseUrl + '/api/myurl')

Silicon answered 19/1, 2022 at 8:19 Comment(0)
C
0

Another cause for this error could be that in your code, you might be referring to an environment variable as:

const localhostUrl = process.env.LOCALHOST_URL;

Whilst, not having this variable defined, (as expected), in your .env file as:

LOCALHOST_URL = http://localhost:8000/
Capeskin answered 8/6, 2022 at 10:59 Comment(0)
C
0

I also got same error when when using supertest and jest. fix was adding prefix "/" to the url.

Churchwell answered 27/4, 2023 at 9:4 Comment(0)
I
0

This error could also be caused by a prerender error according to Next.js 13 documentation.

In my case, I was assuming all values existed, instead of setting a fallback value which should be null.

So I changed:

{articles.map((article) => {
return (
<div key = {article.id}>
<h1>{article.title}</>
</>
)
})}

TO:

{articles ? articles.map((article) => {
return (
<div key = {article.id}>
<h1>{article.title}</>
</>
)
}): null}

Unfortunately, this error did not show up at build time, it showed only after deploying to Vercel.

Itu answered 14/11, 2023 at 12:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.