/ HTTP Status Codes

101 Switching Protocols

The 101 Switching Protocols status code is an HTTP/1.1 status code that indicates the server understands and is willing to comply with the client’s request to switch to a different protocol. This status code is typically used when a client requests to upgrade the connection to a higher-level protocol, such as WebSocket.

In this article, we will explore the purpose of the 101 Switching Protocols status code, its usage, and provide examples of a client request and server response.

Purpose of the 101 Switching Protocols Status Code

The primary purpose of the 101 Switching Protocols status code is to allow a client and server to agree on a protocol upgrade. The client initiates the request, and if the server supports the requested protocol, it responds with a 101 Switching Protocols status code to confirm the switch.

By upgrading the protocol, the client and server can take advantage of the features provided by the new protocol, such as full-duplex communication in the case of WebSocket.

Usage of the 101 Switching Protocols Status Code

The most common use case for the 101 Switching Protocols status code is when a client requests to switch from HTTP to WebSocket. The WebSocket protocol enables real-time, bidirectional communication between the client and server, which is not possible with basic HTTP.

To request a protocol upgrade, the client sends an HTTP request with the Upgrade header field, specifying the desired protocol. The server then checks if it supports the requested protocol and, if so, sends a 101 Switching Protocols response.

Example: Requesting a Protocol Upgrade

Here’s an example of a client request to upgrade the connection to WebSocket:

GET /chat HTTP/1.1
Host: example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
Origin: http://example.com

In this example, the client sends a GET request to the /chat endpoint. The Upgrade header field specifies websocket as the desired protocol, and the Connection header field includes the Upgrade token to indicate that the client wants to upgrade the connection.

The Sec-WebSocket-* headers are specific to the WebSocket protocol and provide additional information required for the protocol handshake.

Example: Server Response to a Protocol Upgrade Request

If the server supports the requested protocol, it will respond with a 101 Switching Protocols status code and include the appropriate headers:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat

In this example, the server confirms the protocol upgrade by including the Upgrade and Connection header fields in the response. The Sec-WebSocket-Accept header field is used to confirm the WebSocket handshake, and the Sec-WebSocket-Protocol header field indicates the chosen subprotocol (in this case, chat).

Summary

The 101 Switching Protocols status code is an essential part of the HTTP/1.1 protocol, allowing clients and servers to upgrade their connection to a higher-level protocol. The most common use case is upgrading from HTTP to WebSocket, enabling real-time, bidirectional communication.

By understanding the purpose and usage of the 101 Switching Protocols status code, you can effectively implement protocol upgrades in your web applications and take advantage of the benefits provided by higher-level protocols like WebSocket.

Was this helpful?

Thanks for your feedback!