1. Home
  2. Explore
  3. Computer Science
  4. Understanding REST APIs

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Understanding REST APIs

🌐 Understanding REST APIs

A REST API (Representational State Transfer Application Programming Interface) is an architectural style for designing networked applications. It enables different software systems to communicate with each other over the internet, typically using HTTP (Hypertext Transfer Protocol). This approach treats server-side objects as resources that can be created, read, updated, and deleted (CRUD operations).

💡 What is an API?

Before diving into REST, it's crucial to understand what an API (Application Programming Interface) is in general.

  • Definition: An API is a contract provided by one piece of software to another, allowing them to interact. It specifies how software components should interact.

  • Structure: It usually involves a structured request from one software and a structured response from another.

  • Scope: APIs are broad, existing in operating systems, smartphones, and web applications. We are specifically focusing on web APIs.

  • Analogy 1: Restaurant Waiter:

    • You (Client): A web application requesting information.

    • Kitchen (Server): The service that processes requests and holds data.

    • Waiter (API): The messenger. The waiter is "formatted" to take a specific order (request) and bring back a specific meal (data/response).

  • Analogy 2: Shape Sorter Toy:

    • Toy (API): Formatted to accept specific shapes (requests).

    • Client: Must format its request as a circle, square, or triangle.

    • Error: Any other shape (unformatted request) will result in an error.

    • Standard: The shape represents the API standard (e.g., JSON, SOAP).

    • Output: Successful input results in an output (response).

🚀 REST: Representational State Transfer

REST is an architectural style for designing networked applications that relies on a stateless client-server communication protocol, almost exclusively HTTP.

  • Core Principle: REST treats objects on the server side as resources that can be manipulated using standard HTTP methods.

  • HTTP as Foundation:

    • HTTP is the communication foundation of the web. Every time a web page loads, an HTTP request is made.

    • REST leverages HTTP's built-in delivery methods for its operations.

  • Statelessness: Each request from a client to a server contains all the information needed to understand the request. The server does not store any client context between requests.

  • Resource-Oriented: Server-side objects (e.g., a blog post, a user, a product) are considered resources.

  • Flexibility: Because REST uses standard HTTP and common data formats like JSON (JavaScript Object Notation), it can be used by virtually any programming language (PHP, JavaScript, Python, Java, etc.) that can make HTTP requests.

  • "RESTful API": This term refers to an API that conforms to REST constraints, meaning "REST API" and "RESTful API" are essentially interchangeable.

🛠️ HTTP Methods in REST (CRUD Operations)

REST APIs primarily use standard HTTP methods to perform CRUD (Create, Read, Update, Delete) operations on resources.

HTTP Method

Purpose

CRUD Operation

Description

Example Use Case

GET

Retrieve data

Read

Used to request data from a specified resource. It's the most common request, used by browsers daily to load web pages. Cannot send data in body.

Fetching a list of all users, retrieving details of a single blog post.

POST

Submit data

Create

Used to send data to a server to create a new resource. Commonly used when filling out web forms.

Submitting a new user registration, creating a new blog post.

PUT

Update/Replace data

Update

Used to update a specified resource, usually by replacing the entire resource with new data. Requires sending an ID to specify which resource to update. Cannot be made directly from HTML forms; requires AJAX or framework HTTP modules.

Updating all fields of a user's profile.

DELETE

Remove data

Delete

Used to delete a specified resource on the server. Requires sending an ID to specify which resource to delete. Cannot be made directly from HTML forms; requires AJAX or framework HTTP modules.

Deleting a specific product from an inventory.

HEAD

Get header info only

-

Same as GET, but only returns the response headers, not the body. Rarely used.

Checking if a resource exists or its metadata without downloading the full content.

OPTIONS

Get supported methods

-

Used to describe the communication options for the target resource. Rarely used.

Determining what HTTP methods (GET, POST, PUT, DELETE) are allowed on a specific endpoint.

PATCH

Update partial resources

Update

Used to apply partial modifications to a resource. Rarely used.

Updating only a specific field (e.g., changing only a user's email, not their entire profile).

Important Note: GET and POST are the only methods that can be made directly from standard HTML web forms. PUT and DELETE require more advanced techniques like AJAX (Asynchronous JavaScript and XML) or HTTP modules in frameworks like Angular.

📍 Endpoint Examples

Endpoints are the URIs (Uniform Resource Identifiers) or URLs where HTTP requests are sent. They define the location of the resources.

Consider a sample API at mysite.com/api.

| Method | Endpoint Example | Description | | /api/users | GET: Returns a list of all users. | Read | | | /api/users/:id | GET: Returns the specified user. | Read | | | /api/users | POST: Creates a new user. | Create | | | /api/users/1 | PUT: Updates the user with ID 1. | Update | | | /api/users/1 | DELETE: Removes the user with ID 1. | Delete | | | /api/users/update/1 | PUT: Updates the user with ID 1. | Update | | | /api/users/delete/1 | DELETE: Removes the user with ID 1. | Delete | | | /api/users/delete/1 | DELETE: Removes the user with ID 1. | Delete | | | /api/users/delete/1 | DELETE: Removes the user with ID 1. | Delete | | | /api/users/delete/1 | DELETE: Removes the user with ID 1. | Delete | | | /api/users/delete/1 | DELETE: Removes the user with ID 1. | Delete | | | /api/users/delete/1 | DELETE: Removes the user with ID **1. | **Delete** | | | /api/users/delete/1` | DELETE: Removes the user with ID 1. | Delete | | | REST API | Client-server: Communication is stateless, meaning each request contains all necessary information. | Statelessness | HTTP: The foundation of web communication, used for nearly all REST API interactions. | | Explanation: An API is a contract between two pieces of software. It specifies how to make a request (the input, like a specific shape) and what to expect as a response (the output, like a specific meal). REST builds on this by standardizing these interactions using HTTP methods and defining resources.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent