HTTP conditional requests

The conditional requests feature offered by HTTP allows clients to ask the server whether it has an updated copy of the resource by including some extra header parameters in the request. The server will return the resource back to the client only if the resource that the client has is not up to date.

Conditional requests can be implemented either by comparing the hash value of resource contents (via the ETag header) or by checking the time at which the resource is modified (via the Last-Modified header). Here is the quick summary of the request and response header parameters used in conditional requests:

  • ETag: When requesting for a resource, the client adds an ETag header, containing a hash or checksum of the resource that the client has received from the server for the last request. This hash value should change whenever the resource representation changes on the server. This allows the server to identify whether the client-cached contents of the resource are different from the actual content at the server.
  • Last-Modified: This header works similar to ETag, except that it uses the timestamp for validating the cache. While generating a response, the server generates the Last-Modified header for the resource and passes it to the client. Subsequent requests from the client can send this timestamp information to the server via the If-Modified-Since request header, which tells the server not to send the resource again if it has not been changed.
You can use the mentioned conditional approach while modifying a resource as well. This will help you ensure that the update is not happening against a stale instance of the resource. Your RESTful web API can be designed to throw 412 Precondition Failed if the ETag header sent by the client does not match the ETag header generated on the server using the latest contents.

To enable conditional requests, JAX-RS offers APIs such as javax.ws.rs.core.EntityTag and javax.ws.rs.core.Request::evaluatePreconditions(), the former to generate ETag and the latter to evaluate the preconditions present in the request, such as If-Modified-Since and If-None-Match. To learn the usage of the ETag and Last-Modified headers in a JAX-RS application, you can refer to the Conditional request processing with the Last-Modified HTTP response header and Conditional request processing with the ETag HTTP response header sections in Chapter 4, Advanced Features in the JAX-RS APIs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.24.30