API Development
- API development using GET, POST, PUT, and DELETE methods with structured request and response handling.
๐น Get, Post, Put, Delete APIs
HTTP methods define what action the client wants to perform on the server. In RESTful API development, each method has a specific purpose and follows a standard behavior.
GET โ Fetch Data
Used to retrieve data from the server.
Does not change server data.Example:
GET /users
Response:
GET API Request and JSON Response
This example shows how a frontend application requests user data from a backend API using a GET /users request. The server responds with a JSON array containing user information, which the frontend can display or process further.
[
{ "id": 1, "name": "Amit" },
{ "id": 2, "name": "Neha" }
]
POST โ Create Data
Used to send data to the server and create a new resource.
Example:
POST /users
Request Body:
POST Method โ Create Data
The POST method is used to send data from the client to the server to create a new resource. In this example, a POST /users request sends user details in the request body, allowing the server to create and store a new user record.
{ "name": "Rahul", "email": "rahul@gmail.com" }
- Response:
POST Method Response โ User Created
This response confirms that the server has successfully created a new user after receiving a POST request. It returns the newly assigned user ID along with the userโs details, indicating that the data has been stored correctly.
{ "id": 3, "name": "Rahul" }
PUT โ Update Data
Used to update existing data completely.
Example:
PUT /users/3
Request Body:
PUT Method โ Update Data
The PUT method is used to completely update an existing resource on the server. In this example, the PUT /users/3 request updates the userโs name by sending new data in the request body, replacing the previous information.
{ "name": "Rahul Sharma" }
DELETE โ Remove Data
Used to delete a resource from the server.
Example:
DELETE /users/3
Response:
DELETE Method โ Remove Data
The DELETE method is used to remove an existing resource from the server. In this example, the DELETE /users/3 request deletes the specified user, and the server responds with a confirmation message indicating successful deletion.
{ "message": "User deleted successfully" }
๐น Request & Response Handling
Request and Response handling is the process of receiving client data, processing it on the server, and sending back a structured response.
Each API call includes:
Request โ Data sent by client
Response โ Data sent by server
Request Components
URL โ API endpoint
Method โ GET / POST / PUT / DELETE
Headers โ Metadata (Authorization, Content-Type)
Body โ Data (mainly for POST & PUT)
Example Request:
Request Components in Server Communication
This section explains the main components of a client request sent to the server. A request includes the URL (API endpoint), HTTP method (GET, POST, PUT, DELETE), headers for metadata like authorization and content type, and an optional body containing data, usually used with POST and PUT requests.
{
"name": "Neha",
"email": "neha@gmail.com"
}
Response Components
Status Code โ Result of request
Data โ JSON response
Message โ Success or error info
Common Status Codes:
200 โ Success
201 โ Resource Created
400 โ Bad Request
404 โ Not Found
500 โ Server Error