101
Switching Protocols
InformationalHTTP 101 Switching Protocols — WebSocket Handshake Explained
The server agrees to switch to the protocol requested by the client in the Upgrade header. Most commonly used when upgrading an HTTP connection to a WebSocket connection.
When to Return 101
When the client sends an Upgrade: websocket (or Upgrade: h2c for HTTP/2 over cleartext) header and the server agrees to switch. The connection protocol changes immediately after this response.
HTTP Response Example
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=Code Examples
Express.js
import { WebSocketServer } from 'ws'
import express from 'express'
import http from 'http'
const app = express()
const server = http.createServer(app)
const wss = new WebSocketServer({ server })
wss.on('connection', (ws) => {
// Connection upgraded — 101 was sent automatically
ws.on('message', (data) => ws.send(`Echo: ${data}`))
})Next.js App Router
// WebSocket servers run outside Next.js App Router.
// Use a custom server.js or a separate WebSocket service.
// The 101 response is handled by the ws/socket.io library.Related Status Codes
Frequently Asked Questions
What does HTTP 101 Switching Protocols mean?
The server agrees to switch to the protocol requested by the client in the Upgrade header. Most commonly used when upgrading an HTTP connection to a WebSocket connection.
When should an API return 101?
When the client sends an Upgrade: websocket (or Upgrade: h2c for HTTP/2 over cleartext) header and the server agrees to switch. The connection protocol changes immediately after this response.
