Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@karthikdivi karthikdivi commented Feb 10, 2017

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

@wachpwnski

This comment has been minimized.

Copy link

@wachpwnski wachpwnski commented Apr 21, 2017

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

This comment has been minimized.

Copy link

@silvioprog silvioprog commented May 4, 2017

@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

This comment has been minimized.

Copy link

@moolitayer moolitayer commented Jul 30, 2017

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

@parayaluyanta

This comment has been minimized.

Copy link

@parayaluyanta parayaluyanta commented Dec 4, 2017

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

This comment has been minimized.

Copy link

@laredotornado laredotornado commented Mar 22, 2018

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

This comment has been minimized.

Copy link

@jageee 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"

@P-Medicado

This comment has been minimized.

Copy link

@P-Medicado P-Medicado commented Mar 30, 2018

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

@vi

This comment has been minimized.

Copy link

@vi 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

This comment has been minimized.

Copy link

@stefanofiorentino stefanofiorentino commented Jan 18, 2019

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

@k-five

This comment has been minimized.

Copy link

@k-five k-five commented Jul 12, 2019

Worked for me. Thanks

response

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

This comment has been minimized.

Copy link

@1zg12 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.

@Lin-Buo-Ren

This comment has been minimized.

Copy link

@Lin-Buo-Ren Lin-Buo-Ren commented May 21, 2020

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

This comment has been minimized.

Copy link

@jasonbutler22 jasonbutler22 commented Jul 31, 2020

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

This comment has been minimized.

Copy link

@randhawp randhawp commented Oct 31, 2020

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

@aubuchcl

This comment has been minimized.

Copy link

@aubuchcl aubuchcl commented Jan 21, 2021

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

This comment has been minimized.

Copy link

@aubuchcl aubuchcl commented Jan 21, 2021

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

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