15

I see that there is websocket endpoint which works out fins with Java tests. In logs I see

Connecting to: ws://127.0.0.1:8080/76f48a44-0af8-444c-ba97-3f1ed34afc91/tweets  

Just like any other REST API I would like to hit it via browser or curl, but when I do that I see

➜  tweetstream git:(master) ✗ curl ws://127.0.0.1:8080/b9b90525-4cd4-43de-b893-7ef107ad06c2/tweets  
curl: (1) Protocol ws not supported or disabled in libcurl  

and

➜  tweetstream git:(master) ✗ curl http://127.0.0.1:8080/b9b90525-4cd4-43de-b893-7ef107ad06c2/tweets
<html><head><title>Error</title></head><body>Not Found</body></html>%  

Is there a way to test websocket APIs with browser/curl?

daydreamer
  • 80,741
  • 175
  • 429
  • 691

7 Answers7

13

This did the trick for me:

$ curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: echo.websocket.org" -H "Origin: http://www.websocket.org" http://echo.websocket.org

from: http://www.thenerdary.net/post/24889968081/debugging-websockets-with-curl

Damian
  • 2,490
  • 22
  • 19
  • 5
    there is no endpoint, it's only testing the root url. I think the original question is about specific endpoints such as ip:port/endpoint/ – Mikael Sep 06 '16 at 09:14
9

If you mean literally to test the implementation of websockets, I found Autobahn's test suite to be very useful: http://autobahn.ws/

If you just want to noodle with a websocket I would recommend using the developer tools in a browser like chrome to make a connection and send/recv data:

var ws = new WebSocket("ws://127.0.0.1:8080/76f48a44-0af8-444c-ba97-3f1ed34afc91/tweets");
ws.onclose = function() { // thing to do on close
};
ws.onerror = function() { // thing to do on error
};
ws.onmessage = function() { // thing to do on message
};
ws.onopen = function() { // thing to do on open
};
ws.send("Hello World");
Lee Adams
  • 301
  • 1
  • 7
5

I had to use this command to make it work:

$ curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: echo.websocket.org" -H "Origin: http://www.websocket.org" -H "Sec-WebSocket-Version: 13" -H 'Sec-WebSocket-Key: +onQ3ZxjWlkNa0na6ydhNg==' http://www.websocket.org

I am using Jetty, and if I didn't add the Sec-WebSocket-Version/Sec-WebSocket-Key doesn't work. Just for the record.

facundofarias
  • 2,867
  • 25
  • 27
3

For completeness, I'd like to add my own CLI tool: websocat.

$ websocat wss://echo.websocket.org/
qwer
qwer
1234
1234

It does not do the "browser" part of the question, but should be a valid substitute for "curl" in this case.

Vi.
  • 33,936
  • 16
  • 92
  • 141
1

I have used this and found it to be quite simple

http://www.websocket.org/echo.html

1
curl --include \
--no-buffer \
--header "Connection: Upgrade" \
--header "Upgrade: websocket" \
--header "Host: echo.websocket.org" \
--header "Origin: http://echo.websocket.org" \
--header "Sec-WebSocket-Version: 13" \
http://echo.websocket.org

Add the header if you have an additional sub-protocol configured for the WebSocket endpoint:

--header "Sec-protocol: protocol-name"

Replace thee Origin, Host and endpoint to test with your own server. If you have basic auth enabled, add the Authorization header like this:

 --header "Authorization: Basic RVZCLVAxNzI2MzAzOv"
0

I wrote a cURL like WebSocket client. You can send single data frame (text/binary) via this tool and then it closes the socket. Also you can add HTTP headers in the initial HTTP upgrade process.

https://github.com/jussmen/WebSocket_cURL

  • If you have a new question, please ask it by clicking the [Ask Question](http://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/low-quality-posts/13281443) – Castro Roy Aug 09 '16 at 21:12
  • @CastroRoy, hmm, appears as a legitimate answer although I didn't test it working. – akostadinov Oct 04 '16 at 14:03
  • Anyone just finding this, the application does not work or no longer works, tested with python2 and python3. – Paul J May 04 '21 at 19:07