Rest APIs
by Tomas TrescakΒ· APIs

Root Folder
Not Attempted
NameProgress
Introduction
Not Read
🍰 Player
Not Read
Setup
Not Attempted
NameProgress
Software Installation
Not Read
Project Setup
Not Read
Running and Testing
Not Read
React and ReactDOM
Not Read
πŸ’‘ Assignment 1: Welcome Message
Not Attempted
Submissions
Not Read
React
Not Attempted
NameProgress
JSX and Components
Not Read
Props
Not Read
πŸ‘Ύ Exercise: Props
Not Attempted
CSS Styles
Not Read
useState and Hooks
Not Read
πŸ‘Ύ Exercise: useState
Not Attempted
Conditional Rendering
Not Read
Lists
Not Read
πŸ‘Ύ Exercise: Lists
Not Attempted
Forms and Events
Not Read
πŸ‘Ύ Exercise: Forms
Not Attempted
πŸ’‘ Assignment 2: Front End
Not Attempted
Advanced React
Not Attempted
NameProgress
Pure Components - memo
Not Read
Lifecycle - useEffect
Not Read
Expensive? - useMemo
Not Read
DOM Interactions - useRef
Not Read
forwardRef
Not Read
useImperativeHandle
Not Read
πŸ‘Ύ useImperativeHandle
Not Attempted
Context
Not Read
useCallback
Not Read
useId
Not Read
useReducer
Not Read
Infrastructure
Not Attempted
NameProgress
Database
Not Read
NextAuth and Github Authentication
Not Read
Prisma and ORM
Not Read
Project Setup
Not Read
Project Authentication
Not Read
APIs
APIs - Slides
Not Attempted
Rest APIs
Rest APIs - Express.js
ReastAPIs - Next.js
Securing APIs
Securing APIs - NextAuth
tRPC
Not Attempted
NameProgress
tRPC
Not Read
tRPC - Routers
Not Read
tRPC - Server Rendering
Not Read
tRPC - Client Rendering
Not Read
Persisting Data
Not Read
Assignment 3: APIs
Not Read
0 / 60 XP

REST APIs, or Representational State Transfer Application Programming Interfaces, are a popular way to create web services that communicate over HTTP. They are designed to be simple, stateless, and resource-based, making them an efficient way for different software systems to exchange data. Let's explore what REST APIs are, why they are widely used, and how to work with them through examples.

Key Principles of REST APIs

  1. Statelessness: Every request from a client to the server must contain all the information needed for the server to fulfill it. The server does not retain any session information between requests, making REST APIs scalable.

  2. Uniform Interface: The API should have a consistent and predictable structure. REST uses standard HTTP methods like GET, POST, PUT, and DELETE, each mapping to a particular operation.

  3. Resource-Based: The main concept of REST is that it treats everything as a resource (users, products, posts) that can be uniquely identified via a URL (Uniform Resource Locator).

  4. Client-Server Architecture: The client (frontend) and server (backend) are separate entities that communicate via the REST API, allowing independent development and scaling.

HTTP Methods in REST

  • GET: Retrieve data from the server.

  • POST: Submit new data to the server.

  • PUT: Update existing data or create new data.

  • DELETE: Remove existing data.

REST API Endpoint Examples

To illustrate how REST APIs work, let's consider a simple scenario where we manage a list of books.

πŸš€ GET /posts

Retrieve a list of all available tasks:

GET https://api.example.com/tasks

Response (JSON):

[
  {"id": 1, "title": "Clean the room"},
  {"id": 2, "title": "Cook a dinner"}
]

πŸš€  GET /task/{id}

Retrieve a specific task by its ID:

GET https://api.example.com/task/1

Response (JSON):

{
   "id": 1, 
   "title": "Clean the room", 
   "due": "1/4/2024", 
   "state": "INCOMPLETE"
}

πŸš€  POST /task

Add a new task to the list. The POST requests allow you to provide a body (payload) with data. The Post request then generates a response.

POST https://api.example.com/books

Request Payload (JSON):

{"title": "Wash a car" }

Response: A response indicating success, along with the newly created resource ID:

{"id": 3, "message": "Task added successfully"}

πŸš€  PUT /task/{id}

Update an existing book's information: PUT also allows to provide a request body (payload)

PUT https://api.example.com/books/1

Request Payload (JSON):

{"id": 3, "status": "COMPLETED"}

Response:

{"message": "Task updated successfully"}

πŸš€  DELETE /task/{id}

Remove a task from the list:

DELETE https://api.example.com/books/1

Response:

{"message": "Book deleted successfully"}

Benefits of REST APIs

  • Scalability: Statelessness ensures that the server doesn’t need to remember previous requests, allowing horizontal scaling.

  • Interoperability: By using standard HTTP methods, REST APIs can be consumed by any client, regardless of platform or programming language.

  • Maintainability: Uniform interfaces and consistent structure make REST APIs easier to document and maintain.

Drawbacks of REST APIS

While REST APIs are highly popular and provide many benefits, they also have some drawbacks that developers should be aware of when choosing an API architecture. Here are a few notable limitations:

  1. Overfetching and Underfetching:

    • Overfetching occurs when a client receives more data than it needs in response to an API call. For example, requesting an entire user object might also return fields that aren't necessary for the current task.

    • Underfetching happens when an API call does not return all required information, forcing the client to make multiple requests to get the complete data.

  2. Type Safety
    The APIs do impose a specific format of inputs and outputs. But, the client does not learn about any change until an error occurs. Thus, it would be better to have a mechanism that would allow for knowing, how any API change affects existing codebase.

  3. Statelessness Limitations:
    While statelessness makes scaling easier, it also requires that each request contains all the information needed for processing. This can lead to larger payloads and increase the number of requests if the same data is required repeatedly.

  4. Lack of Flexibility in Data Queries:
    REST APIs rely on fixed endpoints, which makes it difficult to retrieve data in complex or nested structures. In cases where clients need to customize their data queries dynamically, this lack of flexibility can be problematic.

  5. Performance Issues:

    • Multiple Round Trips: Clients often need to call multiple endpoints to gather related data, resulting in additional network round trips and increased latency.

    • Heavy Payloads: Depending on the complexity of the resource, responses can be bulky, slowing down network transmission and increasing processing times

Conclusion

REST APIs offer a flexible and efficient way to connect different systems over the web. By understanding their principles and practicing the usage of HTTP methods and endpoints, you can design, implement, and consume REST APIs to build powerful applications that interact smoothly with other services.

Maggie

Discuss with Maggie
Use the power of generative AI to interact with course content

Discussion

0 comments
Loading editor ...
Remember to be polite and report any undesirable behaviour

Category

Empty

Labels

Discussion has no labels

1 participant

user avatar

Priority

Notifications

You're not receiving notifications from this thread.
Course Outline