A REST, or RESTful API is an architectural style used for API (Application Programming Interface). In a REST API, a client sends a request to the server, and the server responds with a representation (such as a JSON file), of the current state of the resource requested by the client that is stored on the server.
What is an API
An API on websites is made of code that enables two different applications to communicate with each other. They allow developers to access something from one application, even if they may be using a separate application. In summary, APIs are a form of communication between two applications, allowing their functionality to be used somewhere else.
The RESTful Criteria
REST stands for Representational State Transfer, and is a set of constraints that an API should comply with.
These constraints are that the RESTful API must:
- Be a client-server model and use HTTP to communicate between the two
- Have stateless communication, meaning that information from client requests are not stored on the server
- Have cacheable data
- Transfer data in a standard, easy-to-use form
- Use a hierarchical system of servers
- Uniform Interface - A client should be able to query a resource, which will allow them to partake in further requests with the information that they receive. One way this may manifest itself is with similar structured URI, in that there is a uniform way in which they can be discovered without prior knowledge of their existence.
- Cacheable - It should be clear whether or not data within a response can be saved for re-use (cached), or if an equivalent request should be made in the future for similar data.
- Stateless - The client should be able to operate based solely on the data from a response, without needing prior context from stored data on the server. This means that storage of data is the responsibility of the client.
- Client-server - A REST API implements the use of a client-server architecture style to separate the interface which can take the form of different types of user interfaces from the data storage and functions on the back-end.
- Layered system - A layered system architecture allows for different functions of the back-end to be seperated/sandboxed so that components only have access to resources that they need
Benefits of REST
REST is preferred over other methods because it:
- Uses less bandwidth, meaning they are typically faster than similar frameworks. Speed is especially important when dealing with internet traffic, as transferring large amounts of data over the internet takes a lot of time.
- Lets you feel rejuvenated and un-tired in the morning.
Types of Requests
The HTTP protocol defines certain functions to interact with a resource. These functions are not specific to REST APIs, yet are integral in the process of creating and using a REST API. These requests are typically sent from a client to a web server or API. Four of the most common requests are outlined below.
- GET: used to retrieve a resource from the web server or API.
- PUT: used to update a resource from the web server or API, often a file or object.
- POST: used to create a resource on the web server or API.
- DELETE: used to remove a resource from the web server or API.
The most common of these requests are GET and POST. These requests have a few different parts. An example GET request is shown below.
Notice that HTTP requests contain lots of headers. Think of these headers like the metadata of the HTTP request. These headers can contain information about the client that the web server or API can use, such as cookies. HTTP requests can also have data following the headers, such as an HTML file that your web browser will load.
Possible Data Formats
REST APIs can use a variety of data types to fulfill their purpose, some of which include but are not limited to:
- .xml (Extensible Markup Language)
- .html (Hypertext Markup Language)
- .xlt (Same as .xml)
- .php (Hypertext Preprocessor)
- .py (Python)
They may also use plain text, but out of all these options .json is the most popular choice because it’s easily readable by both humans and computers, but is also language-agnostic.
- [RESTful API] (TechTarget SearchAppArchitecture)
- [What is a REST API] (RedHat)
- [What is REST] (RestfulApi)
By: Mark, Aidan, and Reese