REST WEBSERVICE

Using HTTP Methods: CRUD Operations

Now we will see how to use the HTTP methods to perform the basic crud operations in the registration app.

Suppose we want to GET the data of the student with student id 10. The resource URI for this operation will be /myApp/students/10.

And if we want to DELETE the data of the student with the student id 10. The resource URI for this operation will be /myApp/students/10

Now how will the server identify that which request is for the GET operation and which request is for the DELETE operation for the same URI. The answer is, by checking the HTTP method parameter. The method for the first operation should be GET and for the second operation, it should be DELETE.

The common HTTP methods are:

  • GET
  • POST
  • PUT
  • DELETE

There are some other methods that are rarely used like HEAD and OPTIONS. These HTTP methods have specific meaning and you typically use the right method for the right operation. Also while surfing the Internet, like when you are browsing a webpage you are actually using these methods automatically. When you type a URL in the address bar of your browser, the browser automatically issues a GET request to the browser. When u submit an HTML Form, the browser uses the POST request to do so. The idea is to use the right method depending on the operation.

When we talk about REST application, you do not name methods like, getStudents. You make a GET request for the /myApp/students/ URI.


Use Cases for our Registration Application

There are certain use cases of Resgistration Application like:

Getting the Student/Course Information

This should be getting the resource URI.

GET   /myApp/students/      // This will give the list of all the students in the system.
GET  /myApp/students/1     // This will give the student with the roll number 1.

Similarly we can get the course information

GET  /myApp/courses/         // This wil return the list of all the courses of the students.
GET /myApp/registrations/   // this will give all the registrations information.

Deleting the Student/Course Information

To delete any student or course,

DELETE /myApp/students/1      // This will delete the student record with the roll number 1.
DELETE /myApp/couses/10      // This will delete the course with course id 10.

Updating the Student/Course Information

To update student or course information,

PUT /myApp/students/1   //This will update the student record with the roll number 1.
PUT /myApp/courses/10  //This will update the course record with course id 10.

The data to be updated, will go in the body of the HTTP POST request.


Inserting the Student/Course Information

This is really important as now we have to create a new entry for the student or course information.

The request in this case should be sent to the collection URI and not to the single resource, for example:

POST  /myApp/students/20    //This is incorrect
POST /myApp/students/      //This is correct

So for creating a new resource we should always send the request to the collection URI and leave the responsibility of creating the roll no. or course id to the application.

So when we do post request, POST /myApp/students/ with the request body, the application will create a new resource and generate a new roll number and return this to the user as response.


As of now, we have only learnt, how to operate on a single resource. But what if we information about all of the students, courses and registrations. This is a Collection URI.

For Example:

GET /myApp/students/           //This will return all the records of the students.
GET /myApp/courses/            //This will return all the records of the courses.
GET /myApp/registrations/     //This will return all the records of all the registrations.

So in this lesson we have identified the basic HTTP methods which can meet our requirements when designing an API as these HTTP methods map to the CRUD operations.