Chapter 6. Asynchronous REST

Asynchronous operations might be one of the most complex aspects of RESTful architecture. Imagine performing a certain action on a resource that will take a considerable amount of time to finish. Should you leave the client to wait until the action has finished and you are able to return a meaningful HTTP code? In this chapter we will see what the best strategy is in situations like this one, and what codes your API should return when you want to tell the client that you will perform the operation at later time.

Asynchronous RESTful Operations

The HTTP protocol is synchronous. When an HTTP request is made to a server, the client expects an answer, whether it indicates success or failure (see Figure 6-1).

Figure 6-1. A simple REST request and response—the client has performed a POST request to a resource and the server has returned the 201 (Created) status code

Yet the fact that the server has returned an answer does not mean, per se, that the action or actions initiated by the request have to finish immediately. For example, you might request an operation that requires some time or resources to complete, and these might not be available at the moment the request is made.

This could very well be the case for a service that processes images or videos or audio files. In such a situation, the server usually accepts the request made by the client and ...

Get RESTful Rails Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.