101 Switching Protocols
Fault: Neither
TL;DR
The server understands and is willing to comply with the client's request to switch protocols.
The server is switching protocols as requested by the client.
What This Error Means
The server is switching protocols as requested by the client.
The server understands and is willing to comply with the client's request to switch protocols.
Common Causes
- WebSocket upgrade request
- HTTP/2 upgrade
- Protocol switch requested via Upgrade header
How to Fix It (For Visitors)
- This is not an error - protocol upgrade in progress
How to Fix It (For Developers/Admins)
- Ensure proper WebSocket handshake implementation
- Handle protocol upgrades correctly
Code Examples
Here's how to return a 101 status code in various programming languages:
Python (Flask)
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/endpoint')
def endpoint():
return jsonify({"message": "Switching Protocols"}), 101
Node.js (Express)
const express = require('express');
const app = express();
app.get('/endpoint', (req, res) => {
res.status(101).json({ message: 'Switching Protocols' });
});
PHP
<?php
http_response_code(101);
header('Content-Type: application/json');
echo json_encode(['message' => 'Switching Protocols']);
?>
Go
package main
import (
"encoding/json"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(101)
json.NewEncoder(w).Encode(map[string]string{
"message": "Switching Protocols",
})
}
Java (Spring Boot)
@GetMapping("/endpoint")
public ResponseEntity<Map<String, String>> endpoint() {
Map<String, String> response = new HashMap<>();
response.put("message", "Switching Protocols");
return ResponseEntity.status(101).body(response);
}
Ruby (Sinatra)
get '/endpoint' do
status 101
json message: 'Switching Protocols'
end
Browser Compatibility
| Browser | Support | Notes |
|---|---|---|
| Chrome | ✓ Full Support | All versions |
| Firefox | ✓ Full Support | All versions |
| Safari | ✓ Full Support | All versions |
| Edge | ✓ Full Support | All versions |
Official Specification
This status code is defined in RFC 7231 Section 6.2.2.