RESTful:- There are some most useful HTTP methods that must be known by a developer. RESTful APIs are those that follow(Representational State Transfer) style. RESTful methods are working on the server side using the JavaScript language. The RESTful web services are created using JavaScript because its uses in the client-side as well as server-side programming.
- GET :- The get method is used to get a particular record from the collection of number of records from the database. Here is one code example of a GET method.
const getStudents = async(URL) => {
const response = await fetch(URL);
const data = await response.json();
console.log(data)
}
const response = await fetch(URL);
const data = await response.json();
console.log(data)
}
getStudents(BASEURL"/students");
The above example shows that the get() retrieves the student details and a function() contains its two objects one is for request and other one is for response.
2. POST :- The post method is utilized to create new data on the database or server. It adds or puts the data on the server as per its response.
const addStudent = async (URL, student) => {
const response = await fetch(URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: student,
});
const data = await response.json();
const response = await fetch(URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: student,
});
const data = await response.json();
console.log(data.message);
};
};
addStudent(BASEURL + "/students", { id: 3, name: "Geek3" })
The above example shows that the post() puts the data and displays the message that the record will be added successfully on the students records.
3. PUT :- The put method is used to update an existing data. The put method is similar to SQL update query both are used for updating the data on the server.
const updateStudent = async (URL, student) => {
const response = await fetch(URL, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: student,
});
const data = await response.json();
console.log(data.message);
};
updateStudent(BASEURL + "/students/3", { id: 3, name: "Geek3 Update});
const response = await fetch(URL, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: student,
});
const data = await response.json();
console.log(data.message);
};
updateStudent(BASEURL + "/students/3", { id: 3, name: "Geek3 Update});
The above example shows using put() in JavaScript that updates the record of students through its ID and displays the message Geek3 Update.
4. PATCH :- The patch method works similarly like put() used for updating the record on the server. But the major difference between them is patch() partially updating the record instead of updating the whole record.
const updateStudentPatch = async (URL, student) => {
const response = await fetch(URL, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
body: student,
});
const data = await response.json();
console.log(data);
};
updateStudentPatch(BASEURL + "/students/2", { name: "Geek2 Updated using Patch" });
5. DELETE :- The name says about this. It deletes the record(s) from the server. Similarly like SQL delete query. Both are used to delete a record from the database or server.
const deleteStudent = async (URL) => {
const response = await fetch(URL, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
});
const data = await response.json();
console.log(data);
};
deleteStudent(BASEURL + "/students/3");
Leave a comment: