๐น Introduction
If you are learning web development, youโve probably heard the term API many times. But what exactly is a REST API, and how does it work?
In this guide, Iโll explain REST APIs in a simple and beginner-friendly way with real examples
=====================================================
๐น What is an API?
API stands for Application Programming Interface.
It allows two applications to communicate with each other.
๐ Example:
When you use a weather app, it fetches data from a server using an API.
=====================================================
๐น What is a REST API?
REST (Representational State Transfer) is a way to design APIs using standard HTTP methods.
It is widely used in modern web development because it is simple, scalable, and efficient.
=====================================================
๐น Common HTTP Methods
Method Use GET Fetch data POST Send data PUT Update data DELETE Remove data
=====================================================
๐น Simple Example (JavaScript)
๐น 1. GET (Fetch Data)
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(data => console.log(data));
๐ Use: Get data from server
๐ Example: Fetch all posts
๐น 2. POST (Send Data)
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
title: 'New Post',
body: 'This is a new post',
userId: 1
})
})
.then(response => response.json())
.then(data => console.log(data));
๐ Use: Create new data
๐ Example: Add a new post
๐น 3. PUT (Update Data)
fetch('https://jsonplaceholder.typicode.com/posts/1', {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: 1,
title: 'Updated Title',
body: 'Updated content',
userId: 1
})
})
.then(response => response.json())
.then(data => console.log(data));
๐ Use: Update existing data (replace full resource)
๐น 4. DELETE (Remove Data)
fetch('https://jsonplaceholder.typicode.com/posts/1', {
method: 'DELETE'
})
.then(() => console.log('Post deleted'));
๐ Use: Delete data
๐ Example: Remove a post
๐น Real-Life Example
Imagine a food delivery app:
GET โ View menu
POST โ Place order
DELETE โ Cancel order
=====================================================
๐น Why REST APIs are Important
Used in almost all modern applications
Connect frontend and backend systems
Essential skill for every developer
=====================================================
Top comments (0)