101 Switching Protocols
Description
The 101 Switching Protocols
status code indicates that the server understands and is willing to comply with the client's request to change the application protocol being used on this connection. The server will switch protocols immediately after sending the 101 response.
This status code is commonly used in scenarios where a client wants to upgrade from HTTP/1.1 to a different protocol such as WebSocket, HTTP/2, or other protocols using the same TCP connection. The client initiates this process by sending an Upgrade
header field in the request, specifying which protocol it wishes to switch to.
The 101 Switching Protocols
status enables seamless protocol transitions, allowing for more efficient communication methods when both the client and server support them.
Syntax
The server responds with a 101 Switching Protocols
status and includes headers indicating the new protocol:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
[Additional protocol-specific headers]
[empty line]
After this response, the server immediately begins communicating using the new protocol.
Examples
WebSocket Upgrade Example
A client wants to establish a WebSocket connection:
Client Request:
GET /chat HTTP/1.1
Host: example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Origin: https://example.com
Server Response:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
After this response, the TCP connection is now used for WebSocket protocol communication rather than HTTP.
HTTP/2 Upgrade Example
A client wants to upgrade to HTTP/2:
Client Request:
GET / HTTP/1.1
Host: example.com
Connection: Upgrade, HTTP2-Settings
Upgrade: h2c
HTTP2-Settings: AAMAAABkAAQAAP__
Server Response:
After this response, the connection switches to HTTP/2 protocol.
Summary
The 101 Switching Protocols
status code facilitates protocol transitions within an existing connection. It's a key mechanism for enabling modern web technologies like WebSockets and HTTP/2 when starting from an HTTP/1.1 connection. The client initiates the protocol change by including appropriate Upgrade
and Connection
headers, and the server confirms the switch with the 101 response before immediately beginning communication using the new protocol.