Expect
Description
The Expect
request header is used by clients to indicate that certain
conditions must be met by the server before proceeding with a request. This
header is commonly used to specify expectations about how the server should
handle the request, ensuring that the server is capable of fulfilling specific
requirements before processing the request body.
The most common use of the Expect
header is with the 100-continue
directive,
which allows a client to send request headers first and wait for confirmation
from the server before transmitting a potentially large request body. This helps
prevent unnecessary data transmission when a server is unable to process the
request.
Syntax
The syntax of the Expect
header follows this structure:
<expectation>
: A condition that the client requires the server to meet, such as100-continue
.
Example Syntax
Examples
Using 100-continue
to Prevent Unnecessary Data Transfer
A client making a POST
request with a large payload can use 100-continue
to
verify that the server is willing to accept the request before sending the body:
If the server is ready to accept the request, it responds with:
After receiving 100 Continue
, the client proceeds to send the request body:
If the server cannot fulfill the request, it may respond with an error status instead, preventing the client from sending the body unnecessarily:
Handling Expect
Header in a Server Response
If a client sends an expectation the server cannot meet, it responds with a 417
Expectation Failed
status:
HTTP/1.1 417 Expectation Failed
Content-Type: text/plain
The expectation cannot be fulfilled by the server.
Summary
The Expect
request header allows clients to specify conditions that the server
must meet before proceeding with a request. The most common use case is
100-continue
, which helps optimize network usage by ensuring that large
request bodies are only sent if the server is prepared to process them. Proper
implementation of this header enhances efficiency and prevents unnecessary data
transmission in HTTP communications.