Huachao / vscode-restclient

REST Client Extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=humao.rest-client
MIT License
5.13k stars 426 forks source link

Connection is being rejected for localhost #523

Open heneds opened 4 years ago

heneds commented 4 years ago

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

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

patr1kt0th commented 4 years ago

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.

Huachao commented 4 years ago

@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

patr1kt0th commented 4 years ago

@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?

Huachao commented 4 years ago

@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.

patr1kt0th commented 4 years ago

@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" }

Huachao commented 4 years ago

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

Huachao commented 4 years ago

@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

patr1kt0th commented 4 years ago

@Huachao Yeah, it works ;)

vaclavbenes commented 4 years ago

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
Huachao commented 4 years ago

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

vaclavbenes commented 4 years ago

@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.

Huachao commented 4 years ago

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

vaclavbenes commented 4 years ago

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/drash@v0.41.1/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"
Huachao commented 4 years ago

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

vaclavbenes commented 4 years ago

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

vaclavbenes commented 4 years ago

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

Hatgor commented 4 years ago

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.

Huachao commented 4 years ago

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

Hatgor commented 4 years ago

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

Thanks! It's working now =)

lucasluca commented 3 years ago

Im facing the same issue in MacOs Catalina

mensand commented 3 years ago

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

interglobalmedia commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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!

mohadel92 commented 3 years ago

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

thanks

buyaoshushu commented 3 years ago

@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.

leviethung2103 commented 3 years ago

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

nikitajaiswal0902 commented 3 years ago

@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.

hyperloo commented 3 years ago

@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

snyang commented 3 years ago

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.
vediatoni commented 3 years ago

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.

rpsingh009 commented 3 years ago

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 ?

daniel-filipe-lobo commented 3 years ago

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

kmylo commented 3 years ago

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

RajaTheKing826 commented 2 years ago

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

jenkinsvctrn commented 2 years ago

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

kimcatsb commented 2 years ago

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 commented 2 years ago

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.
soheilous commented 2 years ago

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"

kimcatsb commented 2 years ago

@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.

aderchox commented 2 years ago

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.

mitaloammon commented 2 years ago

only run the application in your terminal 🤷‍♂️

kayleeliyx commented 1 year ago

I met the same error. However, it doesn't work for my case. No matter localhost or 127.0.0.1 Could you please help with this? @Huachao I have

jvannistelrooy commented 1 year ago

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.

I'm running into the the above issue too. Converting localhost to 127.0.0.1 breaks this extension for me on MacOS Ventura. It would be great if localhost would not be converted anymore.

deepakChourasiya-aj commented 1 year ago

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:9090.

/so what is the solution me also showing same error

mrmign commented 1 year ago

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:40601.

fhdiaze commented 1 year ago

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.

Thanks

IP

Hi, thanks it worked for me.

FlaviodosSantos commented 1 year ago

De alguma forma eu consertei.@kimcatsbestá completamente certo. O problema será corrigido para usuários do WSL2 se esta extensão parar de se transformar localhostem 127.0.0.1. Quando o localhost se transformar em 127.0.0.1, ele apontará para o sistema operacional Windows real, ou seja, o host do WSL2 e não o próprio WSL2 vm.

Existem várias soluções para isso. O mais rápido é executar ip a | grep inetno WSL2 e usar seu endereço IP real. Assim, seus pedidos se tornarão algo assim:

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

No entanto, esta solução não é boa, pois o endereço IP do WSL2 não é estático (ele mudará). Outra solução seria usar o cmd do Windows, que é um pesadelo para muitas pessoas unixy. Então eu também pergunto@Huachaopara corrigir esse problema para usuários do WSL2. Obrigado.

Thank you very much

Hamada-Ali commented 1 year ago

for those who still have this issue :

solution 1 :

solution 2 :