Explain different HTTP methods supported by RESTful web services

Collapse

Unconfigured Ad Widget

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • Rex Maughan
    Senior Member
    • Mar 2022
    • 110

    Explain different HTTP methods supported by RESTful web services

    Hello everyone,

    Anyone has any information regarding the different HTTP methods that supported by the RESTful web services then do let me know in the comment.
  • wisly.k
    Member
    • May 2022
    • 99

    #2
    Hello Rex here I'm sharing the information regarding HTTP methods supported by RESTful web services:


    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.
    1. 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)
    }
    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();
    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});

    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" });
    The above example shows how the function will make a patch req. And the fetch function is to return the answer and update some value of the students data.

    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");
    The above example shows how the function deleteStudent() holds the req. URL and response will get the value of that function that we want to delete.


    Comment

    Working...
    X