Skip to content

Instantly share code, notes, and snippets.

@htp
Last active March 25, 2026 18:35
Show Gist options
  • Select an option

  • Save htp/fbce19069187ec1cc486b594104f01d0 to your computer and use it in GitHub Desktop.

Select an option

Save htp/fbce19069187ec1cc486b594104f01d0 to your computer and use it in GitHub Desktop.
Test a WebSocket using curl.
curl --include \
--no-buffer \
--header "Connection: Upgrade" \
--header "Upgrade: websocket" \
--header "Host: example.com:80" \
--header "Origin: http://example.com:80" \
--header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" \
--header "Sec-WebSocket-Version: 13" \
http://example.com:80/
@realyukii

realyukii commented May 27, 2025

Copy link
Copy Markdown

okay, changing Sec-WebSocket-key with the one provided from browser works (not sure why):

curl --include \
     --no-buffer \
     --header "Connection: Upgrade" \
     --header "Upgrade: websocket" \
     --header "Host: localhost:8080" \
     --header "Origin: http://localhost:8080" \
     --header "Sec-WebSocket-Key: 3tMWYzcHwmObiLR3nWOgsg==" \
     --header "Sec-WebSocket-Version: 13" \
     http://localhost:8080/
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: YLxhg+bEzrZXCaOyyI5QkRBIdT4=

�       something

EDIT:
after reading the wikipedia, turned out it just a random 16-byte encoded with base64, on Linux, you can generate it with:

head -c 16 /dev/urandom | base64

@enderandpeter

Copy link
Copy Markdown

This was very helpful. Thanks so much. In my case I just needed to confirm that the websocket server could be connected to and the connection would be upgraded, but I see how the headers and other settings can be changed as needed for any particular situation.

@rajsek

rajsek commented Mar 25, 2026

Copy link
Copy Markdown

Just got to know about this so posting here to help others

WebSocket has been supported by curl as a non-experimental feature since version 8.11.0 (November 6 2024). With the upcoming release of version 8.16.0, we are taking it a step further.

With that release, you can use WebSocket in a pipe from the command line. Like this:

curl --no-progress-meter -T . -N wss://echo.websocket.org/
Request served by 4d896d95b55478dsadsa
Hello!
Hello!
you just echo what I send?
you just echo what I send?
...

This command line contacts the “echo” server run by websocket.org which just sends back what it receives from the client. The command line options are:

-T .: tells curl to upload the data from stdin, in non-blocking fashion.
-N: to avoid buffering of output data, so that even a single character received from the server is on stdout right away.
--no-progress-meter: to avoid curl cluttering stderr with information about how much was sent/received.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment