/ HTTP Headers

Expect

The HTTP Expect header is an essential part of the Hypertext Transfer Protocol (HTTP) request, used to indicate specific behaviors that the client expects from the server while processing the request.

The Expect Header and Its Use Case

The primary use case for the Expect header is to implement the “100-continue” behavior. The “100-continue” behavior allows a client to send a request with the Expect: 100-continue header and wait for the server’s permission (in the form of a 100 Continue status) before sending the request body. This mechanism is particularly useful when dealing with large request bodies, as it enables the client to ensure that the server is willing to accept the request before actually transmitting the potentially large payload.

The general syntax for the Expect header is as follows:

Expect: 100-continue

It is important to note that the Expect header is not limited to the “100-continue” behavior. However, other expectations are not standardized and might not be supported by all servers.

Example: Using the Expect Header with 100-continue

In this example, we will demonstrate how the Expect header can be used with the “100-continue” behavior in an HTTP request and the corresponding server response.

HTTP Request

Consider a client wanting to send a large file to a server using the POST method. The client can include the Expect: 100-continue header in the request to ensure that the server is willing to accept the file before sending the actual data.

POST /files/upload HTTP/1.1
Host: example.com
Content-Type: application/octet-stream
Content-Length: 1024000
Expect: 100-continue

HTTP Response

Upon receiving the request, the server can either:

  1. Respond with a 100 Continue status, indicating that the client should proceed with sending the request body, or
  2. Respond with a different status, such as 417 Expectation Failed, to indicate that the server cannot meet the client’s expectations.

Server Response: 100 Continue

If the server is willing to accept the request, it will send a 100 Continue status to the client:

HTTP/1.1 100 Continue

After receiving the 100 Continue status, the client can proceed with sending the request body.

Server Response: 417 Expectation Failed

If the server cannot meet the client’s expectations, it may respond with a 417 Expectation Failed status:

HTTP/1.1 417 Expectation Failed
Content-Type: text/plain
Content-Length: 44

The server cannot meet the client's expectations.

Upon receiving a 417 Expectation Failed status, the client should not send the request body and should instead handle the error.

Summary

The HTTP Expect header is a valuable tool for controlling the behavior of HTTP requests, particularly when dealing with large request bodies. By using the “100-continue” behavior, clients can ensure that a server is willing to accept a request before actually transmitting the payload. This can help save bandwidth and processing resources, and improve the overall efficiency of the client-server communication.

Was this helpful?

Thanks for your feedback!