This is part 1 of 2 articles explaining the basic concepts of REST.
What you should know before reading this article:
You should have some understanding of what is HTTP and what is an API.
REST is an architectural style, or design pattern, for APIs.
Who invented REST?
REST was defined by Roy Fielding, a computer scientist. He presented the REST principles in his PhD dissertation in 2000.
Before we dive into what makes an API RESTful and what constraints and rules you should follow if you want to create RESTful APIs, let?s explain 2 key terms:
- Client ? the client is the person or software who uses the API. It can be a developer, for example you, as a developer, can use Twitter API to read and write data from Twitter, create a new tweet and do more actions in a program that you write. Your program will call Twitter?s API. The client can also be a web browser. When you go to Twitter website, your browser is the client who calls Twitter API and uses the returned data to render information on the screen.
- Resource ? a resource can be any object the API can provide information about. In Instagram?s API, for example, a resource can be a user, a photo, a hashtag. Each resource has a unique identifier. The identifier can be a name or a number.
Now let?s get back to REST.
A RESTful web application exposes information about itself in the form of information about its resources. It also enables the client to take actions on those resources, such as create new resources (i.e. create a new user) or change existing resources (i.e. edit a post).
In order for your APIs to be RESTful, you have to follow a set of constraints when you write them. The REST set of constraints will make your APIs easier to use and also easier to discover, meaning a developer who is just starting to use your APIs will have an easier time learning how to do so.
REST stands for REpresentational State Transfer.
It means when a RESTful API is called, the server will transfer to the client a representation of the state of the requested resource.
For example, when a developer calls Instagram API to fetch a specific user (the resource), the API will return the state of that user, including their name, the number of posts that user posted on Instagram so far, how many followers they have, and more.
The representation of the state can be in a JSON format, and probably for most APIs this is indeed the case. It can also be in XML or HTML format.
What the server does when you, the client, call one of its APIs depends on 2 things that you need to provide to the server:
- An identifier for the resource you are interested in. This is the URL for the resource, also known as the endpoint. In fact, URL stands for Uniform Resource Locator.
- The operation you want the server to perform on that resource, in the form of an HTTP method, or verb. The common HTTP methods are GET, POST, PUT, and DELETE.
For example, fetching a specific Twitter user, using Twitter?s RESTful API, will require a URL that identify that user and the HTTP method GET.
Another example, this URL: www.twitter.com/jk_rowling has the unique identifier for J. K. Rowling?s Twitter user, which is her username, jk_rowling. Twitter uses the username as the identifier, and indeed Twitter usernames are unique ? there are no 2 Twitter users with the same username.
The HTTP method GET indicates that we want to get the state of that user.
Continue to Part 2 to learn about the 6 REST constraints.