101 Switching Protocols
The HTTP 101 Switching Protocols informational
response status code indicates the
HTTP session continues and the server is
switching to a new protocol. This response is sent
when the client includes the Upgrade
request header along with the required
Connection: upgrade header.
Usage
Clients including the Upgrade request header want the server to use a different protocol for the remainder of the HTTP session. Multiple protocols are listed on a single line, comma-delimited, in order of descending preference. The protocol version is optional.
Connection: upgrade
Upgrade: protocol-name[/protocol-version]
The request is made when there is a benefit to doing so, such as taking advantage of features offered by more recent protocol versions. This upgrade is only supported by HTTP/1.1, and a client has no way to force the server to change protocols. The upgrade is optional, and the server returns 200 when the Upgrade header is ignored.
Server-side request
The server has no way to force a client to upgrade protocols, but the server refuses requests when a client fails to meet requirements. When a client sends a request using a protocol the server does not support, the server returns 426. Using the protocol directed by the server, the client has subsequent requests accepted and processed.
Example
The client requests an upgrade from HTTP/1.1 to the WebSocket protocol. The server accepts and switches protocols.
Request
GET /chat HTTP/1.1
Host: www.example.re
Connection: upgrade
Upgrade: websocket
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Response
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
Code references
.NET
HttpStatusCode.SwitchingProtocols
Rust
http::StatusCode::SWITCHING_PROTOCOLS
Rails
:switching_protocols
Go
http.StatusSwitchingProtocols
Symfony
Response::HTTP_SWITCHING_PROTOCOLS
Python3.5+
http.HTTPStatus.SWITCHING_PROTOCOLS
Apache HttpComponents Core
org.apache.hc.core5.http.HttpStatus.SC_SWITCHING_PROTOCOLS
Angular
@angular/common/http/HttpStatusCode.SwitchingProtocols
Takeaway
The 101 Switching Protocols informational response status code is sent by the server to inform the client the protocol in the HTTP session is switching as requested, and the new protocol is specified.