0% found this document useful (0 votes)
14 views5 pages

REST API (2)

Uploaded by

Anikesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

REST API (2)

Uploaded by

Anikesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

(2708) What is REST API ?

| REST API Explained in 9 mins in Hindi | REST


API Tutorial | Great Learning - YouTube

What is a REST API?

A REST API (Representational State Transfer Application Programming Interface) allows two
systems to communicate over the internet using HTTP methods like GET, POST, PUT, and
DELETE. It’s a way for software applications to interact and exchange data in a structured,
standardized format.

Example in Simple Words

Imagine an online shopping app.

● Frontend: The app you see on your phone (user interface).


● Backend: The server where the app's data (like products, orders) is stored.

The REST API is like a waiter in a restaurant:

1. You (Frontend): Ask the waiter (REST API) for "pizza" (data).
2. Waiter (API): Goes to the kitchen (Backend) and gets the pizza (data).
3. Waiter (API): Brings the pizza (data) back to you.

Technical Example: REST API for a To-Do App

1. GET Request: Fetch all tasks

● URL: https://api.todoapp.com/tasks
● Response:

json
[
{ "id": 1, "task": "Buy groceries", "completed": false },
{ "id": 2, "task": "Study REST API", "completed": true }
]
2. POST Request: Add a new task

● URL: https://api.todoapp.com/tasks
● Request Body:

json
{ "task": "Complete project", "completed": false }

3. PUT Request: Update a task

● URL: https://api.todoapp.com/tasks/1
● Request Body:

json
{ "task": "Buy groceries", "completed": true }

4. DELETE Request: Delete a task

● URL: https://api.todoapp.com/tasks/1
● Response:

json
{ "message": "Task deleted successfully" }

Key Features of REST API

1. Stateless: Each request is independent; no session is maintained.


2. Resource-based: Everything is treated as a resource (e.g., tasks, users).
3. Uses Standard HTTP Methods:
○ GET: Retrieve data.
○ POST: Create new data.
○ PUT: Update existing data.
○ DELETE: Remove data.
4. Structured Data: JSON or XML format.

Real-World Example

● Google Maps API: Helps integrate location-based features into apps.


● Weather API: Fetches weather data for a given city.
REST APIs make it easy for apps and services to connect and exchange data in a simple,
efficient way.

Basic Questions

1. What is a REST API, and how does it work?

● REST API (Representational State Transfer) enables communication between the


client and server using standard HTTP methods like GET, POST, PUT, and DELETE.
● How it works: REST treats everything as a resource, identified by URLs. The server
sends structured responses (e.g., JSON), and each request is independent, maintaining
statelessness.

2. What HTTP methods did you use in your project, and why?

● GET: To fetch messages, users, and conversation details.


● POST: To create new messages or user accounts.
● PUT: To update user profiles or mark messages as read.
● DELETE: To remove conversations or messages.

3. How do you structure your REST API endpoints for the chat app?

● Examples:
○ GET /api/messages/:conversationId: Fetch all messages in a
conversation.
○ POST /api/messages: Send a new message.
○ GET /api/users/:userId: Fetch user details.

Intermediate Questions

4. How do you handle authentication in your REST API?

● I use JWT (JSON Web Tokens):


○ On login, a JWT is generated and sent to the client.
○ Each protected endpoint checks the token in headers or cookies using
middleware to verify user identity.

5. How do you manage errors in your REST API?


● Use appropriate status codes:
○ 401: Unauthorized (e.g., invalid token).
○ 404: Not Found (e.g., missing resource).
○ 500: Internal Server Error.
● Provide meaningful error messages in JSON format:

json

Copy code

{ "error": "User not authenticated" }

6. What type of data format does your REST API return, and why?

● I use JSON because:


○ It’s lightweight, easy to parse, and widely supported across programming
languages.

7. How did you test your REST APIs?

● I used Postman for:


○ Sending requests and verifying responses.
○ Testing authentication by passing JWTs in headers.
● Automated Tests: Tools like Mocha and Chai for backend tests.

8. How do you handle rate-limiting or API throttling in your chat app?

● Implement rate-limiting using middleware like express-rate-limit to prevent abuse.


○ Example: Allow a maximum of 100 requests per minute per user.

Advanced Questions

9. How do you ensure real-time functionality in a REST API-based chat app?

● REST APIs are used for initial data fetching (e.g., user info, conversations).
● For real-time messaging, I use Socket.io with WebSockets to push messages instantly
to connected clients.
10. How do you secure sensitive information in API requests?

● Key practices:
○ Use HTTPS for secure communication.
○ Store passwords securely using bcrypt for hashing.
○ Avoid exposing sensitive data like tokens in URLs or responses.

You might also like