REST API (2)
REST API (2)
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.
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.
● 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 }
● URL: https://api.todoapp.com/tasks/1
● Request Body:
json
{ "task": "Buy groceries", "completed": true }
● URL: https://api.todoapp.com/tasks/1
● Response:
json
{ "message": "Task deleted successfully" }
Real-World Example
Basic Questions
2. What HTTP methods did you use in your project, and why?
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
json
Copy code
6. What type of data format does your REST API return, and why?
Advanced Questions
● 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.