What is REST?
What is REST?
REST API Model
REpresentational State Transfer
REST, orREpresentationalStateTransfer, is an architectural style for providing standards between computer systems on the web, making it easier for systems to communicate with each other. REST-compliant systems, often called RESTful systems, are characterized by how they are stateless and separate the concerns of client and server. We will go into what these terms mean and why they are beneficial characteristics for services on the Web. Pay close attention: If you’re looking for a career in tech, you may be asked to define rest during an interview.
Separation of Client and Server
In the REST architectural style, the implementation of the client and the implementation of the server can be done independently without each knowing about the other. This means that the code on the client side can be changed at any time without affecting the operation of the server, and the code on the server side can be changed without affecting the operation of the client.
As long as each side knows what format of messages to send to the other, they can be kept modular and separate. Separating the user interface concerns from the data storage concerns, we improve the flexibility of the interface across platforms and improve scalability by simplifying the server components. Additionally, the separation allows each component the ability to evolve independently.
By using a REST interface, different clients hit the same REST endpoints, perform the same actions, and receive the same responses.
Statelessness
Systems that follow the REST paradigm are stateless, meaning that the server does not need to know anything about what state the client is in and vice versa. In this way, both the server and the client can understand any message received, even without seeing previous messages. This constraint of statelessness is enforced through the use ofresources, rather thancommands. Resources are the nouns of the Web - they describe any object, document, orthingthat you may need to store or send to other services.
Because REST systems interact through standard operations on resources, they do not rely on the implementation of interfaces.
These constraints help RESTful applications achieve reliability, quick performance, and scalability, as components that can be managed, updated, and reused without affecting the system as a whole, even during operation of the system.
Now, we’ll explore how the communication between the client and server actually happens when we are implementing a RESTful interface.
Communication between Client and Server
In the REST architecture, clients send requests to retrieve or modify resources, and servers send responses to these requests. Let’s take a look at the standard ways to make requests and send responses.
Making Requests
REST requires that a client make a request to the server in order to retrieve or modify data on the server. A request generally consists of:
- an HTTP verb, which defines what kind of operation to perform
- aheader, which allows the client to pass along information about the request
- a path to a resource
- an optional message body containing data
There are 4 basic HTTP verbs we use in requests to interact with resources in a REST system:
- GET — retrieve a specific resource (by id) or a collection of resources
- POST — create a new resource
- PUT — update a specific resource (by id)
- DELETE — remove a specific resource by id
You can learn more about these HTTP verbs in the following Codecademy article:
- What is CRUD?
In the header of the request, the client sends the type of content that it is able to receive from the server. This is called theAcceptfield, and it ensures that the server does not send data that cannot be understood or processed by the client. The options for types of content are MIME Types (or Multipurpose Internet Mail Extensions), which you can read more about in theMDN Web Docs.
MIME Types, used to specify the content types in theAcceptfield, consist of atypeand asubtype. They are separated by a slash (/).
For example, a text file containing HTML would be specified with the typetext/html. If this text file contained CSS instead, it would be specified astext/css. A generic text file would be denoted astext/plain. This default value,text/plain, is not a catch-all, however. If a client is expectingtext/cssand receivestext/plain, it will not be able to recognize the content.
Other types and commonly used subtypes:
- image—image/png,image/jpeg,image/gif
- audio—audio/wav,audio/mpeg
- video—video/mp4,video/ogg
- application—application/json,application/pdf,application/xml,application/octet-stream
For example, a client accessing a resource withid23 in anarticlesresource on a server might send a GET request like this:
GET /articles/23
Accept: text/html, application/xhtml
TheAcceptheader field in this case is saying that the client will accept the content intext/htmlorapplication/xhtml.
Requests must contain a path to a resource that the operation should be performed on. In RESTful APIs, paths should be designed to help the client know what is going on.
Conventionally, the first part of the path should be the plural form of the resource. This keeps nested paths simple to read and easy to understand.
A path likefashionboutique.com/customers/223/orders/12is clear in what it points to, even if you’ve never seen this specific path before, because it is hierarchical and descriptive. We can see that we are accessing the order withid12 for the customer withid223.
Paths should contain the information necessary to locate a resource with the degree of specificity needed. When referring to a list or collection of resources, it is not always necessary to add anid. For example, a POST request to thefashionboutique.com/customerspath would not need an extra identifier, as the server will generate anidfor the new object.
If we are trying to access a single resource, we would need to append anidto the path. For example:GET fashionboutique.com/customers/:id— retrieves the item in thecustomersresource with theidspecified.DELETE fashionboutique.com/customers/:id— deletes the item in thecustomersresource with theidspecified.
Sending Responses
In cases where the server is sending a data payload to the client, the server must include acontent-typein the header of the response. Thiscontent-typeheader field alerts the client to the type of data it is sending in the response body. These content types are MIME Types, just as they are in theacceptfield of the request header. Thecontent-typethat the server sends back in the response should be one of the options that the client specified in theacceptfield of the request.
For example, when a client is accessing a resource withid23 in anarticlesresource with this GET Request:
GET /articles/23 HTTP/1.1
Accept: text/html, application/xhtml
The server might send back the content with the response header:
HTTP/1.1 200 (OK)
Content-Type: text/html
This would signify that the content requested is being returned in the response body with acontent-typeoftext/html, which the client said it would be able to accept.
Responses from the server contain status codes to alert the client to information about the success of the operation. As a developer, you do not need to know every status code (there aremanyof them), but you should know the most common ones and how they are used:
For each HTTP verb, there are expected status codes a server should return upon success:
- GET — return 200 (OK)
- POST — return 201 (CREATED)
- PUT — return 200 (OK)
- DELETE — return 204 (NO CONTENT) If the operation fails, return the most specific status code possible corresponding to the problem that was encountered.
Let’s say we have an application that allows you to view, create, edit, and delete customers and orders for a small clothing store hosted atfashionboutique.com. We could create an HTTP API that allows a client to perform these functions:
If we wanted to view all customers, the request would look like this:
GET http://fashionboutique.com/customers
Accept: application/json
A possible response header would look like:
Status Code: 200 (OK)
Content-type: application/json
followed by thecustomersdata requested inapplication/jsonformat.
Create a new customer by posting the data:
POST http://fashionboutique.com/customers
Body:
{
"customer”: {
"name” = "Scylla Buss”,
"email” = "scylla.buss@codecademy.org”
}
}
The server then generates anidfor that object and returns it back to the client, with a header like:
201 (CREATED)
Content-type: application/json
To view a single customer weGETit by specifying that customer’s id:
GET http://fashionboutique.com/customers/123
Accept: application/json
followed by the data for thecustomerresource withid23 inapplication/jsonformat.
We can update that customer byPUTting the new data:
PUT http://fashionboutique.com/customers/123
Body:
{
"customer”: {
"name” = "Scylla Buss”,
"email” = "scyllabuss1@codecademy.com”
}
}
A possible response header would haveStatus Code: 200 (OK), to notify the client that the item withid123 has been modified.
We can alsoDELETEthat customer by specifying itsid:
DELETE http://fashionboutique.com/customers/123
The response would have a header containingStatus Code: 204 (NO CONTENT), notifying the client that the item withid123 has been deleted, and nothing in the body.
Practice with REST
Let’s imagine we are building a photo-collection site. We want to make an API to keep track of users, venues, and photos of those venues. This site has anindex.htmland astyle.css. Each user has a username and a password. Each photo has a venue and an owner (i.e. the user who took the picture). Each venue has a name and street address. Can you design a REST system that would accommodate:
- storing users, photos, and venues
- accessing venues and accessing certain photos of a certain venue
Start by writing out:
- what kinds of requests we would want to make
- what responses the server should return
- what thecontent-typeof each response should be
Possible Solution - Models
{
"user”: {
"id": <Integer>,
"username”: <String>,
"password”: <String>
}
}
{
"photo”: {
"id": <Integer>,
"venue_id”: <Integer>,
"author_id”: <Integer>
}
}
{
"venue”: {
"id": <Integer>,
"name”: <String>,
"address”: <String>
}
}
Possible Solution - Requests/Responses
Request-GET /index.htmlAccept:text/htmlResponse- 200 (OK) Content-type:text/html
Request-GET /style.cssAccept:text/cssResponse- 200 (OK) Content-type:text/css
Request-GET /venuesAccept:application/jsonResponse- 200 (OK) Content-type:application/json
Request-GET /venues/:idAccept:application/jsonResponse- 200 (OK) Content-type:application/json
Request-GET /venues/:id/photos/:idAccept:application/jsonResponse- 200 (OK) Content-type:image/png
Request-POST /usersResponse- 201 (CREATED) Content-type:application/json
Request-POST /venuesResponse- 201 (CREATED) Content-type:application/json
Request-POST /venues/:id/photosResponse- 201 (CREATED) Content-type:application/json
Request-PUT /users/:idResponse- 200 (OK)
Request-PUT /venues/:idResponse- 200 (OK)
Request-PUT /venues/:id/photos/:idResponse- 200 (OK)
Request-DELETE /venues/:idResponse- 204 (NO CONTENT)
Request-DELETE /venues/:id/photos/:idResponse- 204 (NO CONTENT)
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Complete Payment to Unlock Full Guide & Source Code
You are viewing the 30% preview. Join TopCourseAI Pro to unlock the remaining 70% of this article, unblur all code snippets, and gain full access to all 412 technical projects and guides.
Ready to access the complete step-by-step tutorial?
Gain full unhindered access to all 412 technical projects and guides, unblur all source code snippets, and download runnable scripts.
What is Programming?
Next BlueprintWhat is Retrieval-Augmented Generation (RAG) in AI?
Related Blueprints & Guides
View all 166 →Accessibility on the Platform
Codecademy is committed to providing a high-quality learning experience for all learners. That includes accommodating different usage forms such as auditory, motor, or visual restrictions, limited hardware, or other acce...
Web developmentAffordances, Signifiers, and Clickability
For users on the web, the mouse click is perhaps the most fundamental human-computer interaction. The webbecamethe web partially through the power ofhypertext, or text in one document that links to other documents or res...
Web developmentAuthentication and OAuth
Authenticationis the process used by applications to determine and confirm identities of users. It ensures that the correct content is shown to users. More importantly, it ensures that incorrect content is secured and u...
Ready to build production-grade AI systems?
Enjoyed this guide? Continue exploring our library of 166+ production blueprints, multi-agent swarms, and autonomous agent systems with 100% unblurred, executable source code.