However, it also comes with the trade-off that requests to the server are larger and contain repetitive data when a client has multiple interactions with the server.
HTTP is a protocol that is maintained by the Internet Engineering Task Force. While it is not the same as REST,
it exhibits many features of a RESTful system
. This is not by accident, as Roy Fielding was one of the original authors of RFC for HTTP.
Let’s take a closer look at some of the qualities that make HTTP a RESTful protocol.
In the world of HTTP, resources are typically files on a remote server. These could be HTML, CSS, JavaScript, images, and all the other files that comprise modern web pages.
Each file is treated as a distinct resource that is addressable using a unique URL
.
HTTP is not just for files, though.
The term resource can also refer to arbitrary data on a remote server
: customers, products, configuration settings, and so much more. This is why HTTP has become popular for building modern APIs. It provides a consistent and predictable way to access and manipulate remote data.
Additionally,
HTTP allows clients to choose different representations for some resources
. In HTTP, this is handled using headers and a variety of well-known media types.
For example, a weather web site may provide both HTML and JSON representations for the same weather forecast. One is suitable for displaying in a web browser, while the other is suitable for processing by another system that archives historical weather data.
3.2. HTTP Methods
Another way in which HTTP adheres to the principles of REST is that
it provides the same set of methods for every resource
. While there are nearly a dozen available
HTTP methods
, most services deal primarily with the 4 that map to CRUD operations:
POST
,
GET
,
PUT
, and
DELETE
.
Knowing these operations ahead of time makes it easy to create clients that consume a web service. Compared to protocols such as
SOAP
, where operations can be customized and unlimited, HTTP keeps the known set of operations minimal and consistent.
Of course, individual web services may choose to deny certain methods for some resources. Or they require authentication for sensitive resources. Regardless, the set of
possible
HTTP methods are well known and cannot vary from one web site to another.
3.3. HTTP Is Not Always RESTful
However, for all the ways in which HTTP implements RESTful principles, there are multiple ways in which it can also violate them.
First, REST is not a communications protocol, while HTTP is.
Next, and perhaps most controversially, is that
most modern web servers use cookies and sessions to store state
. When these are used to refer to the state on the server-side, this violates the principle of statelessness.
Finally,
using URLs (as defined by the IETF) might allow a web server to violate the uniform interface
. For example, consider the following URL:
https://www.foo.com/api/v1/customers?id=17&action=clone
While this URL has to be requested using one of the pre-defined HTTP methods such as
GET
, it is using query parameters to provide additional operations. In this case, we’re specifying an action named
clone
that isn’t obviously available to all resources in the system. It’s also not clear what the response would be without more detailed knowledge of the service.
4. Conclusion
While many people continue to use the terms REST and HTTP interchangeably, the truth is that they are different things. REST refers to a set of attributes of a particular architectural style, while HTTP is a well-defined protocol that happens to exhibit many features of a RESTful system.