101 Switching Protocols는 클라이언트가 Upgrade 헤더로 요청한 프로토콜 전환에 서버가 동의했음을 뜻합니다. 이 응답 이후 같은 TCP 연결은 더 이상 일반 HTTP가 아니라 협상된 새 프로토콜로 통신합니다.
현실에서 가장 흔한 용도는 HTTP에서 WebSocket으로의 업그레이드입니다. 응답의 Sec-WebSocket-Accept 값은 클라이언트가 보낸 키를 검증한 결과로, 이때부터 양방향 실시간 통신이 시작됩니다.
GET /chat HTTP/1.1
Host: example.com
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=const ws = new WebSocket('wss://example.com/chat');
ws.onopen = () => ws.send('hello');
// the 101 handshake happens transparently before onopenimport { WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 8080 });
wss.on('connection', s => s.send('welcome')); // library sends 101