Skip to content

Instantly share code, notes, and snippets.

@htp
Last active November 13, 2024 12:46
Show Gist options
  • Save htp/fbce19069187ec1cc486b594104f01d0 to your computer and use it in GitHub Desktop.
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/
@karthikdivi
Copy link

Thanks for the information. Will this curl causes a websocket connection leak if i don't kill the command?

@wachpwnski
Copy link

I keep getting

HTTP/1.1 400 bad Sec-WebSocket-Key (length must be 24 ASCII chars) 'SGVsbG8sIHdvcmxkIQ=='

Where do I get this key from?

@silvioprog
Copy link

@wachpwnski try using this:

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

@moolitayer
Copy link

I keep getting

HTTP/1.1 400 bad Sec-WebSocket-Key (length must be 24 ASCII chars) 'SGVsbG8sIHdvcmxkIQ=='
Where do I get this key from?

You don't need a specific key - the one supplied here should work.
The pourpose of that header is to prevent caching of websocket requests.

See https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake

@prayagupa
Copy link

I get 400 error,

curl -i --no-buffer -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: localhost:9191" -H "Origin: http://localhost:9191" -H "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" -H "Sec-WebSocket-Version: 13" localhost:9191/chat?q=a
HTTP/1.1 400 Bad Request
Server: akka-http/10.0.10
Date: Mon, 04 Dec 2017 08:36:32 GMT
Content-Type: text/plain; charset=UTF-8
Content-Length: 34

Expected WebSocket Upgrade request

It works when I use Dark WSock terminal(a chrome plugin).

Akka Http Endpoint

  val handle: Flow[Message, Message, _] = Flow[Message].map {
    case TextMessage.Strict(event) =>
      sentimentProcessor ? event match {
        case emitted: Seq[ActionResult] =>
          TextMessage(emitted.head)
      }
    case _ => TextMessage("Event type unsupported")
  }

      path("chat") {
        get {
          handleWebSocketMessages(handle)
        }
      }

@laredotornado
Copy link

What if you want to send data on the web socket? Right now it looks like you're just sending a bunch of headers. I assume "--no-buffer" means there is no data being sent to the endpoint.

@jageee
Copy link

jageee commented Mar 27, 2018

this command works for me:
curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: echo.websocket.org" -H "Origin: https://www.websocket.org" https://echo.websocket.org

did anybody try same command using TLS? to "wss://echo.websocket.org"

@MultiCoinCharts
Copy link

@jageee I tried your above command and Mac OS returned, "no URL Specified!" And also https://echo.websocket.org returns 404 if visited.

@vi
Copy link

vi commented Jul 4, 2018

I'd like to advertise my new tool websocat. It is designed to make easy to do tricky things with websockets.

You can check availability of a web socket without transferring any data:

$ websocat -q -uU ws://echo.websocket.org/ 2> /dev/null; echo  $?
0
$ websocat -q -uU ws://echo.websocket.org/404 2> /dev/null; echo  $?
1

@stefanofiorentino
Copy link

@jageee you should just add the "--cacert" cURL option with the CA certificate (pem/der)
curl -i -N --cacert cafile.pem -H ....

@shakibamoshiri
Copy link

Worked for me. Thanks

response

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: qGEgH3En71di5rrssAZTmtRTyFk=

@1zg12
Copy link

1zg12 commented Jan 15, 2020

Thanks for the information. Will this curl causes a websocket connection leak if i don't kill the command?

it's a short-lived connection.

@brlin-tw
Copy link

Just for my convenience:

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/

@jasonbutler22
Copy link

I recommend adding --http1.1 to your curl command. The Connection and Upgrade headers are not valid in http/2 and curl will use http/2 if your server supports it.

I lost a few hours trying to figure out why these headers were disappearing when debugging my httpd web socket rewrite rules for the first time.

@randhawp
Copy link

Once curl opens up the connection to the websocket, how to send other commands

@aubuchcl
Copy link

I recommend adding --http1.1 to your curl command. The Connection and Upgrade headers are not valid in http/2 and curl will use http/2 if your server supports it.

I lost a few hours trying to figure out why these headers were disappearing when debugging my httpd web socket rewrite rules for the first time.

^^ this. and also -o - after the curl command so you dont get the binary output warning.

@aubuchcl
Copy link

curl -o - --http1.1 --include \

@baishi
Copy link

baishi commented Sep 25, 2021

For those who failed the request with Sec-WebSocket-Key, RFC 6455 does require the key to be 16 bytes and the resulting base64 encoded string must be 24 bytes long as @moolitayer quoted. The example from Wikipedia "x3JJHMbDL1EzLkh9GBhXDw==" does a great job. Or simply add some padding to the gist SGVsbG8sIHdvcmxkIQAAAA== also works.

@fenchu
Copy link

fenchu commented Oct 5, 2021

with recent browsers like firefox92, copy as curl in network will add the required settings, you only have to change wss:// to http://

@emwalker
Copy link

did anybody try same command using TLS? to "wss://echo.websocket.org"

Replacing "http://" with "https://" in the curl command seems to do the trick.

Curious whether anyone has gotten a clean termination after the first message, along the lines of websocat --one-message .... I tried using --max-time, but the curl return status ends up being nonzero.

@balusch
Copy link

balusch commented May 31, 2022

I recommend adding --http1.1 to your curl command. The Connection and Upgrade headers are not valid in http/2 and curl will use http/2 if your server supports it.

I lost a few hours trying to figure out why these headers were disappearing when debugging my httpd web socket rewrite rules for the first time.

Thanks, it works!

@o-az
Copy link

o-az commented Mar 22, 2023

curl --include \
   --header "Connection: Upgrade" \
   --header "Upgrade: websocket" \
   --header "Sec-WebSocket-Key: qwerty" \
   http://localhost:8000/

This worked for me. I didn't need all the header items

@dannys42
Copy link

dannys42 commented May 8, 2023

Anyone know if you can use this to send data (i.e. from stdin or a file) through the web socket? I tried -T -, but that doesn't seem to work.

@GeekSnail
Copy link

curl --include \
    -H "Connection: Upgrade" \
    -H "Upgrade: websocket" \
    -H "Sec-WebSocket-Key: qwerty" \
    -H "Sec-WebSocket-Version: 13" \
    https://example.com -k

@marianopeck
Copy link

Hi,

@silvioprog @jageee

I am also using this command:

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

but I am getting a 200 response instead of the 101.

Any ideas?

@vi
Copy link

vi commented Apr 25, 2024

  1. You need to use HTTP/1.1
  2. You have specified too few headers
$ curl --http1.1 -i -N -H 'Sec-Websocket-Version: 13' -H 'Sec-Websocket-Key: QUo86XL2bHszCCpigvKqHg==' -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: echo.websocket.org" -H "Origin: https://www.websocket.org" https://echo.websocket.org
HTTP/1.1 101 Switching Protocols
upgrade: websocket
connection: Upgrade
sec-websocket-accept: 8dlVwoWynsF/RauFo6HjkWl7dLk=
date: Thu, 25 Apr 2024 14:28:01 GMT
server: Fly/610c12e9 (2024-04-23)
via: 1.1 fly.io
fly-request-id: 01HWATFYXA0Y3STERG1SCHNQS2-ams

 Request served by 7811941c69e658

@marianopeck
Copy link

Thanks @vi that worked perfectly!

@tony-caffe
Copy link

I recommend adding --http1.1 to your curl command. The Connection and Upgrade headers are not valid in http/2 and curl will use http/2 if your server supports it.

I lost a few hours trying to figure out why these headers were disappearing when debugging my httpd web socket rewrite rules for the first time.

You saved my life haha. I wasted so much time thinking my move to E2EE was the issue but stupid ActionCable and AWS Target Group using HTTP/2 was the issue! 🤦

Time to look into AnyCable or something else. Thank you!

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