101 Switching Protocols: An HTTP Status Code Indicating Protocol Switching

スポンサーリンク
スポンサーリンク

About 101 Switching Protocols

Overview of 101 Switching Protocols

Indicating Protocol SwitchingHTTP Status Code

101 Switching Protocols

Overview 101 Switching Protocols is an HTTP status code that informs the client that the server is switching communication protocols as requested by the client. This code is returned when the client uses the Upgrade header to request a new protocol, and the server agrees to the switch.

Meaning The server has accepted the request and switched to the specified protocol.

When is 101 Switching Protocols Returned?

  • When the client sends a request with an Upgrade header
  • When the server supports and approves the protocol switch
  • When switching from HTTP to another protocol, such as WebSocket or HTTP/2

Examples of 101 Switching Protocols

Switching from HTTP to WebSocket

GET /chat HTTP/1.1  
Host: example.com  
Upgrade: websocket  
Connection: Upgrade  

Response example:

HTTP/1.1 101 Switching Protocols  
Upgrade: websocket  
Connection: Upgrade  

Explanation: When a client requests to switch to WebSocket using the Upgrade header, and the server approves the request, the 101 status code is returned. Communication then continues using the WebSocket protocol.

Switching from HTTP to HTTP/2

GET / HTTP/1.1  
Host: example.com  
Upgrade: h2c  
Connection: Upgrade  

Response example:

HTTP/1.1 101 Switching Protocols  
Upgrade: h2c  
Connection: Upgrade  

Explanation: When the client requests a switch from HTTP/1.1 to HTTP/2, the server returns 101 if it approves the switch. This is valid only if both the client and server support the protocol.

Points to Note

Important considerations when returning 101 Switching Protocols:

  • Evaluate client requests carefully
    Ensure that the server supports the protocol switch requested by the client.
  • Consider security risks
    Switching protocols, especially to WebSocket, may involve potential security risks and should be configured with care.

Comparison with Related HTTP Status Codes

Explanation of HTTP status codes related to 101 Switching Protocols:

  • 200 OK: Used when the request is successful without changing the communication protocol.
  • 426 Upgrade Required: Returned when the client must upgrade to a protocol supported by the server to complete the request.

Understanding these differences helps ensure the appropriate use of the 101 status code.