101

Switching Protocols

Overview

101 Switching Protocols means the server agreed to the protocol change the client asked for via the Upgrade header. After this response the same TCP connection stops speaking ordinary HTTP and switches to the negotiated protocol.

The most common real-world use is upgrading from HTTP to WebSocket. The Sec-WebSocket-Accept value in the response proves the server validated the client's key, and from that point on the connection carries bidirectional real-time traffic.

When it happens

Request / Response example

Request
GET /chat HTTP/1.1
Host: example.com
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Response
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

In code

const ws = new WebSocket('wss://example.com/chat');
ws.onopen = () => ws.send('hello');
// the 101 handshake happens transparently before onopen
import { WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 8080 });
wss.on('connection', s => s.send('welcome')); // library sends 101

Notes

Related status codes

Related headers

Specification