Upgrade
Description
The Upgrade
request header is used by a client to request an upgrade to a
different communication protocol on an existing connection. It is commonly used
to transition from HTTP/1.1 to WebSockets or HTTP/2 when supported by the
server. The server responds with the 101 Switching Protocols
status if it
agrees to the upgrade request.
This header is often seen in WebSocket communications where an HTTP connection is upgraded to a WebSocket connection.
Syntax
The Upgrade
header follows this syntax:
Where:
<protocol>
is the name of the protocol the client wishes to upgrade to.- Multiple protocols can be specified, separated by commas, indicating preference order.
Examples:
Examples
HTTP to WebSocket Upgrade
A client requests an upgrade to 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
Server Response:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
HTTP/1.1 to HTTP/2 Upgrade
A client requests to upgrade an HTTP/1.1 connection to HTTP/2.
Client Request:
Server Response:
If the server does not support the requested protocol, it will simply ignore the
Upgrade
header and continue using the original protocol.
Summary
The Upgrade
request header allows clients to request a change in communication
protocols, such as upgrading an HTTP connection to WebSockets or HTTP/2. If the
server supports the requested protocol, it responds with a 101 Switching
Protocols
status and confirms the upgrade. This header plays a crucial role in
modern web applications, particularly for real-time communication and improved
efficiency with newer protocols.