vscode-restclient icon indicating copy to clipboard operation
vscode-restclient copied to clipboard

Connection is being rejected for localhost

Open heneds opened this issue 5 years ago • 59 comments

Expectation: If I specify "localhost" in the host address the request should not change to "127.0.0.1".

Some web servers (in this case II Express) do not recognize 127.0.0.1. The server gets the request but is not be able to map against the correct web application.

Symptom:

Connection is being rejected. The service isn’t running on the server, or incorrect proxy settings in vscode, or a firewall is blocking requests. Details: RequestError: connect ECONNREFUSED 127.0.0.1:63353.

See also #122

Steps to Reproduce:

  1. Run a web app from Visual Studio using IIS Express and http
  2. Any REST Client request to localhost will get the response ECONNREFUSED 127.0.0.1
  • VSCode Version: 1.42.0
  • OS Version: Windows 10
  • REST Client Version: 0.23.1
  • Visual Studio Version: 16.4.3 with a .NET Core 2.2 web app

2020-02-12 13_47_05-Home page - WebApplication1

heneds avatar Feb 12 '20 12:02 heneds

I have a similar behavior using REST Client with PHP (Slim Framework). It "worked" before (version 0.22.2), but doesn't anymore (versions 0.23.0 and 0.23.1).

After I start the server using php -S localhost:8080 -t src/, I also get:

Connection is being rejected. The service isn’t running on the server, or incorrect proxy settings in vscode, or a firewall is blocking requests. Details: RequestError: connect ECONNREFUSED 127.0.0.1:8080.

But this "worked" like this before. The difference is that now, once I start the server using php -S 127.0.01:8080 -t src/, I always get a 401 when trying to send a request from REST Client even though it works from my Ionic/Angular application and also after entering the URL directly in the browser.

  • Version: 1.42.1
  • Commit: c47d83b293181d9be64f27ff093689e8e7aed054
  • Date: 2020-02-11T14:44:27.652Z
  • Electron: 6.1.6
  • Chrome: 76.0.3809.146
  • Node.js: 12.4.0
  • V8: 7.6.303.31-electron.0
  • OS: Darwin x64 19.3.0

patr1kt0th avatar Feb 16 '20 13:02 patr1kt0th

@Bilosista What's your auth, why will you get 401? Do you mean you can work with localhost in version 0.22.2 or with 127.0.0.1

Huachao avatar Feb 16 '20 15:02 Huachao

@Huachao It’s Basic auth. No idea why, I can try to check tomorrow (I’m using tuupola/slim-basic-auth), but it works fine when started using 127.0.0.1 with 0.22.2, but doesn’t with 0.23.0 nor 0.23.1. Did something change in 0.23.0?

patr1kt0th avatar Feb 16 '20 18:02 patr1kt0th

@Bilosista I switched my underlying http client package from request to got, as well as some other changes. Can you show me your actual request, especially the line of Authorization header. BTW, if available, please also paste the 401 response details.

Huachao avatar Feb 17 '20 07:02 Huachao

@Huachao Sure, here you go...

The request (I also tried with Authorization: Basic QWRtaW46aGVzbG8=):

GET http://localhost:8080/api/heslo/heslo Authorization: Basic Admin heslo

The 401 response (using version 0.23.1):

HTTP/1.1 401 Unauthorized Host: localhost:8080 Date: Mon, 17 Feb 2020 08:25:03 GMT Connection: close X-Powered-By: PHP/7.3.11 Authorization: Basic QWRtaW46aGVzbG8= WWW-Authenticate: Basic realm="Protected" Access-Control-Allow-Origin: * Access-Control-Allow-Headers: X-Powered-By, X-Requested-With, Content-Type, Accept, Origin, Authorization Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH, OPTIONS Content-type: text/html; charset=UTF-8

The correct response (using 0.22.2):

HTTP/1.1 200 OK Host: localhost:8080 Date: Mon, 17 Feb 2020 08:30:20 GMT Connection: close X-Powered-By: PHP/7.3.11 Authorization: Basic QWRtaW46aGVzbG8= Content-Type: application/json; charset=utf-8 Access-Control-Allow-Origin: * Access-Control-Allow-Headers: X-Powered-By, X-Requested-With, Content-Type, Accept, Origin, Authorization Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH, OPTIONS

{ "heslo": "heslo", "hash": "$2y$10$egJwZRiT958V0isuxEczG.ToT9Un5kk8d3rk4r3AZpxsElKLVsXc6" }

patr1kt0th avatar Feb 17 '20 08:02 patr1kt0th

@Bilosista It seems that I didn't handle the auth challenge case, I will update my code

Huachao avatar Feb 17 '20 11:02 Huachao

@Bilosista can you try this version, and you need to manually remove the .txt in the file name, and install it in the Extensions tab of VSCode by clicking ..., and select Install From VSIX rest-client-0.23.1.vsix.txt

Huachao avatar Feb 17 '20 16:02 Huachao

@Huachao Yeah, it works ;)

patr1kt0th avatar Feb 17 '20 18:02 patr1kt0th

I have same problem on macOs Catalina 10.15.3 with version 0.23.2

and curl from vscode Screenshot 2020-04-27 at 17 32 40

curl from command line

╰─$ curl -H "Content-Type: application/json" --request POST -d '{"name":"Joe Doe"}' --url "http://localhost:8000" ; echo $?
0

vaclavbenes avatar Apr 27 '20 15:04 vaclavbenes

@vaclavbenes can you work with tools like Postman and curl?

Huachao avatar Apr 27 '20 15:04 Huachao

@Huachao. Yes I can. Problem is not in curl or Postman. They work fine. I thought its related to firewall, but i can send POST from curl or postman . I ve tried different versions of client. its same behaviour.

btw: same on Arch Linux.

vaclavbenes avatar Apr 28 '20 10:04 vaclavbenes

@vaclavbenes how can I setup a test environment like you?

Huachao avatar Apr 28 '20 10:04 Huachao

vscode version Version: 1.45.0-insider Commit: abb4a35cfc26102f93fd00df7b59ce1a19c2017a Date: 2020-04-28T05:34:21.109Z (5 hrs ago) Electron: 7.2.2 Chrome: 78.0.3904.130 Node.js: 12.8.1 V8: 7.8.279.23-electron.0 OS: Darwin x64 19.3.0

my server

import { Drash } from "https://deno.land/x/[email protected]/mod.ts";

const docker = {
  image: "localtestimagecentos:latest",
  server: "http://192.168.1.104:8080",
  user: "device",
  verifications: 0,
};

class HomeResource extends Drash.Http.Resource {
  static paths = ["/"];
  public GET() {
    this.response.body = JSON.stringify(docker);
    return this.response;
  }

  public POST() {
    const image = this.request.getBodyParam("name");

    if (!image) {
      throw new Drash.Exceptions.HttpException(
        400,
        "This resource requires the `image` body param.",
      );
    }

    console.log(image);

    return this.response;
  }
}

const server = new Drash.Http.Server({
  response_output: "text/html",
  resources: [HomeResource],
});

const hostname = "localhost";
const port = 8000;

console.log(`http://${hostname}:${port}`);

server.run({
  hostname: hostname,
  port: port,
});

install deno https://deno.land/ run with deno -A example.ts

curl command

curl -H "Content-Type: application/json" --request POST -d '{"name":"Joe Doe"}' --url "http://localhost:8000"

vaclavbenes avatar Apr 28 '20 10:04 vaclavbenes

@vaclavbenes I can't repro with your deno example, can you check your settings in VSCode like http.proxy?

Huachao avatar Apr 28 '20 14:04 Huachao

@Huachao its strange , i removed all settings from user-settings and its no working.

vaclavbenes avatar Apr 30 '20 11:04 vaclavbenes

I did progress. Plugin works with node-js express example, but not working with deno server example. Maybe its related to https://github.com/drashland/deno-drash

vaclavbenes avatar Apr 30 '20 11:04 vaclavbenes

Has similar issue with my node-js express app. My OS is Windows 10, REST Client version is 0.23.2

Server is hosted on localhost:3000

Test request is @url = "http://localhost:3000/" GET {{url}}

Error message:

Connection is being rejected. The service isn’t running on the server, or incorrect proxy settings in vscode, or a firewall is blocking requests. Details: RequestError: connect ECONNREFUSED 127.0.0.1:443.

Postman face no such issues.

Hatgor avatar May 20 '20 21:05 Hatgor

@Hatgor as for your case, the added additional double quotes around the URL, removing it should work.

Huachao avatar May 21 '20 01:05 Huachao

@Hatgor as for your case, the added additional double quotes around the URL, removing it should work.

Thanks! It's working now =)

Hatgor avatar May 21 '20 07:05 Hatgor

Im facing the same issue in MacOs Catalina

lucasluca avatar Aug 01 '20 22:08 lucasluca

I have the same problem also.

The URL is defined as

https://localhost:44391/.well-known/openid-configuration And the error is on https://127.0.0.1:44391/.well-known/openid-configuration

Do you know why the localhost is replaced with 127.0.0.1 IP address. The IISExpress is only listening on localhost by name

It's on windows 10 2004 VS Code

mensand avatar Aug 05 '20 11:08 mensand

Hi, I also have the same problem when trying use POST request on localhost. a GET request on a production url is fine. Just POST request on localhost is causing a problem for me.

I created a .txt file I called rest-client.txt. When I read this thread, I tried to install via vsix, and the file was not recognized. Neither were any other ones on my computer. What could I be doing wrong? Thanks in advance for any help you might be able to provide me! I'm on version 0.24.2.

interglobalmedia avatar Sep 08 '20 23:09 interglobalmedia

I tried again, and now everything for some reason is working. My file is still called rest-client.txt by the way. Now the trick will be to reproduce this!

interglobalmedia avatar Sep 08 '20 23:09 interglobalmedia

Hi, again, so far I have been able to and am starting to get how this works. It's actually much better using this extension that works directly with the code you are building instead of mock third party service code. Thanks!

interglobalmedia avatar Sep 08 '20 23:09 interglobalmedia

Hi again,

Now everything works exactly as expected when I use req.body in my index.js (server file) for my Node.js/Express/MongoDB application. One thing that was causing issues but I didn't check at first was that I would get disconnected from the server and that is why things ended up not working. Now I just have to double check before I hit "Send Request". That must be something either with nodemon or VS Code itself. Thanks!

interglobalmedia avatar Sep 09 '20 10:09 interglobalmedia

@Hatgor as for your case, the added additional double quotes around the URL, removing it should work.

thanks

mohadel92 avatar Oct 24 '20 02:10 mohadel92

@Bilosista can you try this version, and you need to manually remove the .txt in the file name, and install it in the Extensions tab of VSCode by clicking ..., and select Install From VSIX rest-client-0.23.1.vsix.txt

can't sent any curl request on version 0.24.2. downgrade to 0.23.1 anything goes well.

buyaoshushu avatar Oct 28 '20 08:10 buyaoshushu

I am confirming the double quotes would cause the problem. Just simply remove it and everything goes well.

leviethung2103 avatar Nov 01 '20 16:11 leviethung2103

@Huachao I am facing this issue. error

My request is :- POST http://localhost:8091/api/provider/token HTTP/1.1 Content-Type: application/json Authorization: Basic XXXXXXXXXXXXXX

I am sending this request via VS code.

nikitajaiswal0902 avatar Nov 27 '20 09:11 nikitajaiswal0902

@Hatgor as for your case, the added additional double quotes around the URL, removing it should work.

This works for me.

Either declare variable like this without quotes image

or write request url like image

hyperloo avatar Mar 18 '21 07:03 hyperloo

Found same issue via tomcat 9.0.45. My tomcat cannot be accessed via http://127.0.0.1:8080,but localhost:8080 is good. is rest-client converting localhost to 127.0.0.1?

my request is:

###
# @name echo
GET http://localhost:8080/sms/api/echo
Content-Type: application/json;

The error is

The connection was rejected. Either the requested service isn’t running on the requested server/port, the proxy settings in vscode are misconfigured, or a firewall is blocking requests. Details: RequestError: connect ECONNREFUSED 127.0.0.1:8080.

snyang avatar Apr 10 '21 03:04 snyang

Found same issue via tomcat 9.0.45. My tomcat cannot be accessed via http://127.0.0.1:8080,but localhost:8080 is good. is rest-client converting localhost to 127.0.0.1?

my request is:

###
# @name echo
GET http://localhost:8080/sms/api/echo
Content-Type: application/json;

The error is

The connection was rejected. Either the requested service isn’t running on the requested server/port, the proxy settings in vscode are misconfigured, or a firewall is blocking requests. Details: RequestError: connect ECONNREFUSED 127.0.0.1:8080.

Yeah, it looks like it's converting it to 127.0.0.1. I just changed my settings, so now my PHP server now runs on 127.0.0.1 IP and everything works fine.

vediatoni avatar Apr 25 '21 14:04 vediatoni

Found same issue via tomcat 9.0.45. My tomcat cannot be accessed via http://127.0.0.1:8080,but localhost:8080 is good. is rest-client converting localhost to 127.0.0.1? my request is:

###
# @name echo
GET http://localhost:8080/sms/api/echo
Content-Type: application/json;

The error is

The connection was rejected. Either the requested service isn’t running on the requested server/port, the proxy settings in vscode are misconfigured, or a firewall is blocking requests. Details: RequestError: connect ECONNREFUSED 127.0.0.1:8080.

Yeah, it looks like it's converting it to 127.0.0.1. I just changed my settings, so now my PHP server now runs on 127.0.0.1 IP and everything works fine.

how did you change it ?

rpsingh009 avatar May 13 '21 03:05 rpsingh009

For those who waste a whole day trying to find the solution that you think that is a problem with node, or IIS Express, etc. The problem for me was related with this: https://stackoverflow.com/questions/34543443/cant-access-127-0-0-1

daniel-filipe-lobo avatar May 19 '21 21:05 daniel-filipe-lobo

Expectation: If I specify "localhost" in the host address the request should not change to "127.0.0.1".

Some web servers (in this case II Express) do not recognize 127.0.0.1. The server gets the request but is not be able to map against the correct web application.

Symptom:

Connection is being rejected. The service isn’t running on the server, or incorrect proxy settings in vscode, or a firewall is blocking requests. Details: RequestError: connect ECONNREFUSED 127.0.0.1:63353.

See also #122

Steps to Reproduce:

  1. Run a web app from Visual Studio using IIS Express and http
  2. Any REST Client request to localhost will get the response ECONNREFUSED 127.0.0.1
  • VSCode Version: 1.42.0
  • OS Version: Windows 10
  • REST Client Version: 0.23.1
  • Visual Studio Version: 16.4.3 with a .NET Core 2.2 web app

2020-02-12 13_47_05-Home page - WebApplication1

are you running under WSL? I had similar problem and everything worked with CMD https://stackoverflow.com/questions/64763147/access-a-localhost-running-in-windows-from-inside-wsl2

kmylo avatar Jul 01 '21 09:07 kmylo

its not sending the Authorization header for local host url, when I tried to send Authorization header for a lamda gate-way url its working fine, But when i try to send Authoriation header for localhost url its nor working getting 401 Authorization header not included

RajaTheKing826 avatar Aug 24 '21 15:08 RajaTheKing826

the solution was very simple for me. after building the server file, i forgot to run nodejs or nodemone to start/refresh the server. you simply need to initate it before posting requests

adamthegeek avatar Sep 21 '21 06:09 adamthegeek

Spent all day trying to solve this issue but can't seem to get around it issue. Background: I ran my app in WSL2 environment. On my host machine, I can reach it with "localhost:3000", but "127.0.0.1:3000" will not reach it. It has something to do with WSL2 mapping "localhost" to VM processes but not 127.0.0.1.

Is there a way we can prevent REST Client from converting "localhost" into "127.0.0.1"?

kimcatsb avatar Oct 01 '21 09:10 kimcatsb

Answering my own question. I found a solution for my use case. Basically, I run another instance of VisualStudio Code within WSL2 environment.

  1. Open WSL2 (I'm using ubuntu 20.04),
  2. Type code . this will install a copy of Visual Studio Code, open VSC in the directory.
  3. Visual Studio Code window will pop up on the desktop.
  4. Install this REST Client
  5. Create a .rest file
  6. With "localhost"or "127.0.0.1"will reach my service.

kimcatsb avatar Oct 01 '21 12:10 kimcatsb

Im using node server. add this setting to vs code settings.json worked for me : "http.proxy": "https://127.0.0.1:YOUR_SERVER_PORT"

soheilous avatar Oct 24 '21 09:10 soheilous

@S-STALWART-S

My environment is fully working within WSL2, but still want to try other suggestions. I have add the "http.proxy" in settings.json, but I have no idea how to get it to work. Will look into that in future. It's good to know there is alternate solution.

kimcatsb avatar Oct 24 '21 22:10 kimcatsb

I somehow fixed it. @kimcatsb is completely right. The issue will be fixed for WSL2 users if this extension stops turning localhost into 127.0.0.1. When localhost turns into 127.0.0.1, it will point to the actual Windows OS, i.e., the WSL2's host, and not the WSL2 vm itself.

There are multiple solutions to this. The quickest one is, run ip a | grep inet in WSL2 and use its actual IP address. So your requests will become something this:

GET http://172.18.228.135:3000/api/foo

However, this solution isn't great as WSL2's IP address is not static (it will change). Another solution would be using Windows cmd which is a nightmare for many unixy people. So I also ask @Huachao to fix this issue for WSL2 users. Thanks.

aderchox avatar Dec 12 '21 13:12 aderchox

only run the application in your terminal 🤷‍♂️

mitaloammon avatar Mar 04 '22 12:03 mitaloammon