From the course: Learning REST APIs

The RESTful librarian

From the course: Learning REST APIs

Start my 1-month free trial

The RESTful librarian

- To make sense of what a REST API is and how it works, imagine a library with books, a librarian, and people who want to borrow, add, update, or remove those books. On one side, we have the clients. This is the web application, or mobile app, or doorbell, or smartwatch, or whatever interfaces use access to data. On the other side, we have the data store, typically a database, or database server, or some other type of server. In the middle sits our librarian, the REST API, receiving, processing, and handling requests and responses. Here's how our library works. When the client submits a request, in this case to get a resource, the REST API receives that request, identifies the requested resource, figures out what data needs to be gathered, and in what format, creates a representation of the data matching the requested format, bundles it all up with the response header containing metadata, like the resource ID, and hyperlinks to available actions, the media formats this response has when the response was sent and other information, and sends it all back to the client. The client receives the data and parses it into something meaningful on its end, while the REST API sits quietly waiting for the next request. Next, the client, or rather its user wants to change something. These changes are applied to the content received in the original response, and sent back as a put request using the original ID. The REST API receives the request, recognizes this as a put request for an existing resource, notes the requested media format, identifies the requested resource, converts the requested data into a media format that works for the data store, makes the changes submitted, and returns the new representation of the resource along with a success message to notify the client everything went as expected. In this example, we have one client sending requests to the REST API. In most applications we'll have several, maybe hundreds, or thousands, or millions of clients making requests, so the librarian will be very busy. But the actual flow is exactly the same. The client makes the request, the REST API receives the request, gathers and parses the data, and returns that data and the response header to the client.

Contents