101 Switching Protocols

101 Switching Protocols is a status code that’s used for a server to indicate that the TCP conncection is about to be used for a different protocol.

The best example of this is in the WebSocket protocol. WebSocket uses a HTTP handshake when creating the connection, mainly for security reasons.

When a WebSocket client starts the connection, the first few bytes will look like this:

GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Origin: http://example.com
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13

If the server supports WebSocket, it will response with 101 Switching Protocols and then switch to the WebSocket protocol:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
Sec-WebSocket-Protocol: chat

HTTP/2 also uses this mechanism to upgrade from a HTTP/1.1 to a non-ssl HTTP/2 connection. See rfc7540.

HTTP series

This article is part of an ongoing series about the HTTP protocol.

If you want to follow along as I write them, you can Subscribe to my Atom feed or Mailing list.


Web mentions