Rancher API wss connection


I’ve been playing with the Rancher API- Im sending a command to a container to execute some commands in the shell.

I get back my response from the API:
“url”: “wss://XXX.XX.XXX.132:9998/v1/exec/”,
“token”: <>

I then understand, for the commands I have sent to be executed on the container I need to connect with WSS. Here are where my problems start.

Im trying to use a WSS client using the following syntax:

Which is just giving me errors in all the clients I try- I’ve even tried using the request that I captured the Rancher UI and I still cant connect.

Does it look like Im doing anything wrong? I can open a shell in Rancher UI and see Status Code:101 Switching Protocols if I have chrome developer tools open

Also does anyone happen to have an example of using cUrl to connect to the WSS address?

Many thanks in advanced

curl doesn’t really support websockets… If you have nodejs installed npm install -g wscat is an easy client:

wscat -n wss://xxx.xx.xxx.132:9998/v1/exec?token=longstringoftoken

Since you’re getting back wss:// you must have SSL setup, but you have an IP address rather than a hostname. So any certificate check is going to fail, and my guess is your client is requiring that the certificate match. (ignoring that is what -n does for wscat)

Hi Vincent

Thanks for getting back to me.

Yes I do have SSL setup with a self signed cert.

I’ve installed wscat and tried
wscat -c -n wss://XXX.XX.XXX.XXX:9998/v1/exec?token=token_string

Which gives me
error: Error: unexpected server response (401)

Does the token string need to be base 64 encoded?

No it’s just the same token from the previous response. 401 is unauthorized; the tokens are only good for 5 minutes, so you need to generate and use it fairly quickly. And the time on the rancher/server and the host that is running the container you’re connecting to need to be approximately accurate.