WebSocket server health check with curl

Viewed 7537

I am trying to implement a healthcheck for WebSocket server with curl.

curl --include --no-buffer \
    --header "Connection: Upgrade" \
    --header "Upgrade: websocket" \
    --header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" \
    --header "Sec-WebSocket-Version: 13" \
    http://localhost:3000

I send an HTTP request to do the handcheck, and receive an HTTP 101 as expected :

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

My issue is that after receiving that response, curl will just hang.

Is there any way to tell curl to exit after that successful handshake?

1 Answers

You can use Connection: close instead of Connection: Upgrade in order to specify that the connection should be closed after the completion of the response :

curl --include --no-buffer \
    --header "Connection: close" \
    --header "Upgrade: websocket" \
    --header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" \
    --header "Sec-WebSocket-Version: 13" \
    http://localhost:3000
Related