O'Reilly logo

RESTful Web Services Cookbook by Subbu Allamaraju

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

10.2. How to Implement Conditional GET Requests in Servers

Conditional GET requests give the server an opportunity to skip the response body if the representation has not changed since the last time it served the representation. Conditional requests involve the client sending If-Modified-Since and If-Match headers based on the Last-Modified and ETag headers from a previous request. Conditional requests do not cut down on the number of requests from the client, but they can reduce the number of times a server needs to send a fresh representation to the client.

Problem

You want to know how to implement conditional GET requests.

Solution

Design the server to keep track of last modification date-time values and/or entity tags. Include the last-modified date-time as the Last-Modified header and the entity tag as the ETag header when serving representations.

When responding to GET and HEAD requests from the client, if the client has sent an If-None-Match header, compare its value with the ETag of the representation on the server. If the client has sent an If-Unmodified-Since, compare its value with the last-modified time of the representation on the server.

If either checks are false or if the client sent neither of these headers, return the latest copy of the representation to the client including new ETag and/or Last-Modified headers. If not, return HTTP status code 304 (Not Modified) to the client with no message body.

Discussion

The process of using conditional GET requests to extend the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required