postmanlabs / postman-app-support

Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so you can create better APIs—faster.
https://www.postman.com
5.82k stars 839 forks source link

Cannot connect to websocket server running on WSL via "localhost" #11204

Open maxs-rose opened 2 years ago

maxs-rose commented 2 years ago

Is there an existing issue for this?

Describe the Issue

When attempting to connect to a websocket server running within WSL a postman client running on the windows host machine cannot connect to the given endpoint unless given the exact IP of the WSL VM.

Steps To Reproduce

Create a simple websocket server. This example uses the node ws package (https://www.npmjs.com/package/ws) and echos a message to all connected clients

const WebSocket = require("ws");
const wss = new WebSocket.Server({ port: 7071 })

const clients = new Map();

wss.on('connection', (ws) => {
    clients.set(ws);

    ws.on('message', (message) => {
        [...clients.keys()].forEach((client) => {
            client.send(message);
        });
    });
    ws.on("close", () => {
        clients.delete(ws);
    });
});

Connect to the websocket server in a browser running in windows with the native WebSocket client (example in chrome) and messages are received: image

Messages sent from wscat from within wsl: image

Attempting to connect to the same server with postman running in windows results in a connection error if using localhost: image

If using the IP of wsl the connection is successful and messages are received: image

Postman can successfully send plain HTTP requests to WSL with the URL set to localhost

Screenshots or Videos

No response

Operating System

Windows/WSL

Postman Version

9.29.0

Postman Platform

Postman App

Additional Context?

To find your WSL IP you should be able to run wsl hostname -I assuming your are running your websocket server from your default distro. This IP will change on a restart of WSL or the PC.

No response

maxs-rose commented 2 years ago

From looking at the error in postman it seems like it is attempting to convert localhost into 127.0.0.1 while this would probably work in most scenarios since the WSL IP isn't 127.0.0.1 but is accessible through localhost it fails.

NithinSastry commented 1 year ago

Has there been any update on this bug? Is there a tentative release date? I have wasted an hour of my time because of this.

sikkemap commented 1 year ago

Still having this issue too

codenirvana commented 1 year ago

@NithinSastry @sikkemap, can you share a scaled-down server we can use to reproduce this issue?

I am using the one in the issue description, both localhost and 127.0.0.1 work fine.

maxs-rose commented 1 year ago

I haven't tried this in a while but the node server I show in the original description is probably the smallest way to reproduce the issue.

dieeisenefaust commented 1 year ago

I am having the same issue and wasted a good bit of time trying to figure out if it was NestJS or Postman causing the issue.

I am running a simple NestJS websocket server on my WSL Ubuntu instance and trying to connect to it using Postman installed on Windows.

I have multiple Linux distros installed so had to use a slightly different approach to get my IP address ( ip add | grep "eth0" from inside the Linux console) but confirmed the same behavior as reported by the OP.

shamasis commented 1 year ago

We've resolved similar issues around HTTP and localhost earlier and have ensured those cases aren't the cause and that the client doesn't regress for those cases.

I think to resolve this issue we would love to have someone volunteer to get in a call with us and debug it together on the system that's causing this issue.

dieeisenefaust commented 1 year ago

I can meet with you guys. Let me know. Thanks.

Shazway commented 1 year ago

Hi, i've recently started using websockets in NestJs with a gateway set to the 3002 port listening in localhost. After updating Postman and while also running the nestjs server on a WSL I couldn't connect to the websockets anymore with Postman even though it had previously worked perfectly fine. The repository is public, but i'd gladly share my problem on a call too. I will update my comment in case i find a fix. (using my IP adress instead like IP:3002 works fine)

shehabkhalid commented 1 year ago

When using Postman to connect to a WebSocket server, Postman resolves the hostname localhostto the IP address 127.0.0.1by default. This means that when you try to connect to a WebSocket server running on your WSL instance using localhost, Postman actually tries to connect to the WebSocket server running on your Windows machine instead of the one running on WSL.

To solve this problem, you can create a new IP address for your WSL instance by running the command sudo ip addr add 192.168.99.2/24 broadcast 192.168.99.255 dev eth0 label eth0:1 in your WSL terminal. This command creates a new IP address 192.168.99.2 for your WSL instance.

Next, you need to create a new interface in Windows to connect to this new IP address. You can do this by running the command netsh interface ip add address "vEthernet (WSL)" 192.168.99.1 255.255.255.0 in a Windows Command Prompt or PowerShell window. This command creates a new interface named "vEthernet (WSL)" and assigns it the IP address 192.168.99.1.

Now, when you want to connect to the WebSocket server running on your WSL instance using Postman, you can use the new IP address 192.168.99.2 instead of localhost. This will ensure that Postman connects to the WebSocket server running on your WSL instance instead of the one running on your Windows machine.

Hope this solves the problem for you

benkpatr commented 1 year ago

I spent 2 days trying to figure out the problem, just to find out that all I had to do was change the hostname from localhost to 127.0.0.1. Ngl, I still dont get what this is all about, its pretty confusing. I had this problem only with desktop API clients like Postman or Insomnia. Connecting to the websocket through my own client or browser extension worked fine. Edit: Oh, I just realized I might have responded to a wrong thread, as I was running the server on Windows rather than WSL. Nevertheless it seems like the issue is the same.

mnguyen081002 commented 1 year ago

Still having this issue too

Amongash commented 1 year ago

I was facing the same, and this worked for me. Spent hours trying to figure out why it didn't work on postman. I don't mind the approach until they solve the issue

When using Postman to connect to a WebSocket server, Postman resolves the hostname localhostto the IP address 127.0.0.1by default. This means that when you try to connect to a WebSocket server running on your WSL instance using localhost, Postman actually tries to connect to the WebSocket server running on your Windows machine instead of the one running on WSL.

To solve this problem, you can create a new IP address for your WSL instance by running the command sudo ip addr add 192.168.99.2/24 broadcast 192.168.99.255 dev eth0 label eth0:1 in your WSL terminal. This command creates a new IP address 192.168.99.2 for your WSL instance.

Next, you need to create a new interface in Windows to connect to this new IP address. You can do this by running the command netsh interface ip add address "vEthernet (WSL)" 192.168.99.1 255.255.255.0 in a Windows Command Prompt or PowerShell window. This command creates a new interface named "vEthernet (WSL)" and assigns it the IP address 192.168.99.1.

Now, when you want to connect to the WebSocket server running on your WSL instance using Postman, you can use the new IP address 192.168.99.2 instead of localhost. This will ensure that Postman connects to the WebSocket server running on your WSL instance instead of the one running on your Windows machine.

Hope this solves the problem for you

limexp commented 1 year ago

I've got the similar issue without WSL. Every program correctly works with localhost but Postman.

The reason was with localhost that was resolved by Windows to IPv6 address (::1) and not IPv4 (127.0.0.1). Used command ping localhost to found that out. Also the command netsh interface ipv6 show prefixpolicies shows that localhost IPv6 (::1/128) has higher priority than localhost IPv4 (::ffff:0:0/96). Here is an article about changing priority on Windows.

The Postman ignores the IPv6 priority over IPv4 and tries to connect to 127.0.0.1 instead of ::1.

akshaysangma commented 1 year ago

In WSL, run the below command,

 ip add | grep "eth0"

Example output

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1400 qdisc mq state UP group default qlen 1000 inet 172.17.241.208/20 brd 172.17.255.255 scope global eth0

in the above output example, replace the IP 172.17.241.208 as your localhost in your websocket URL in Postman, and you will be able to connect succesfully.

Quality of life Updates on 9/9/2023:

Single line command to directly get the IP:

ip addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'

This will directly return the IP : 172.17.241.208

If you are using ZSH, You can also add this line to your .zshrc to always display the IP address on the right side of your prompt, and then source it.

RPROMPT="$(ip addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}')"

Results: image

Note : run echo $RPROMPT to see your current RPROMPT value if any and update it with this additional cmd.

wilomgfx commented 1 year ago

Same issue here, running under WSL2 (Ubuntu). Directly using the eth0 IP works.

slhmy commented 1 year ago

@NithinSastry @sikkemap, can you share a scaled-down server we can use to reproduce this issue?

I am using the one in the issue description, both localhost and 127.0.0.1 work fine.

@codenirvana I'm not sure if it's working in postman's websocket request, I tried socket.io request and encountered this issue.

Currently I have to use alternative ways like @akshaysangma provided.

I tried the official example from https://github.com/socketio/socket.io/tree/main/examples/chat (with postman’s sock.io request in v3) Will work fine if I use 172.19.49.109:3000 (which is my WSL's IP), but hang on connecting if I use localhost:3000 to connect.

❤️ It will be nice if we can use localhost directly when testing websocket in postman, because always taking addition steps to apply some alternative solution can be confusing for new comers of some project.

virajBaswana commented 11 months ago

A very simple solution that does the job for me. Run the following command in PowerShell . wsl hostname -I In Postman URL bar, replace 'localhost' with the IP received from the above command.

WebSocket connection working great now for me.

piyushyadav1617 commented 11 months ago

A very simple solution that does the job for me. Run the following command in PowerShell . wsl hostname -I In Postman URL bar, replace 'localhost' with the IP received from the above command.

WebSocket connection working great now for me.

This worked fine, thanks.

vimkim commented 9 months ago

Hope this gets fixed..

wajdifarid commented 4 months ago

Still not fixed. But the solution works great.

CrisLi commented 3 months ago

Hope this can get fixed.

Flouwrian commented 2 weeks ago

I also have this issue! Hope it will be fixed in the future!