As discussed so far in the earlier chapter that RESTful web service makes use of HTTP for determining the action to be conceded out on the particular resources. For comprehending the REST structural design, it is exceedingly vital that to understand the HTTP evidently as there is excessive use of the HTTP protocols for building a REST API.
HTTP is everywhere on the internet. Each time you look for some information or resource on the internet, you make use of a URL in your browser where an HTTP request is launched from the client to the web server, and in response, you obtain the HTML content. It is important to note that, while websites are typically for human utilization, the RESTful API deals with application consumption.
More About the HTTP Methods
So, to request data from any target website that data has to be in a browser readable form, this is in HTML, whereas for the REST API, the response will be something related to XML/JSON or in any other compatible type. As REST gets its motivation from HTTP, therefore, it can be said as the stamina and structural pillar of the REST. As you might know that HTTP is a TCP/IP based connection protocol; it is implemented for delivering data across the World Wide Web. REST does not get connected to the web and also does not respond when tried to access using a web browser, even though it is based on HTTP.
Basic HTTP Features for REST
As you know that the HTTP is a protocol that permits us to send and receive files over the internet, using client-server architecture and is easier to monitor. The essential features of HTTP are:
- HTTP is a connectionless protocol.
- HTTP is stateless because both the server and the client keep track of the last request.
- Any data can be transmitted and received via the HTTP protocol, hence type-independent.
For identifying any resources implemented using REST or in general, as well as establishing any connection, HTTP makes use of the Uniform Resource Identifier (URI). HTTP's request and response consist of the following four items:
- A starting line
- One or more headers
- A blank line that indicates the finish of the header field(s)
- And finally an optional message body
CRUD Operations in the HTTP method
Here you will get to know how to use the HTTP methods in REST for performing the basic CRUD operations.
Let suppose you wish to GET or DELETE any data from the student having student ID as 10. The resource URI of such operation will be http://localhost/appName/student/10. Now the catch is how a server can identify which request is associated with GET operation and which one is associated with DELETE for the same URI. It is by checking the HTTP method parameter that will be 'GET', and for the second case, it should be 'DELETE'.
Other methods which are hardly ever used are HEAD and OPTIONS.
Example, to fetch the Student related Information:
|GET||http://localhost/appName/students/||For fetching information for all students.|
|GET||http://localhost/appName/students/10||For fetching the student having roll number 10.|
To delete any record:
|DELETE||http://localhost/appName/student/10||For removing the student record having ID number 10|
|DELETE||http://localhost/appName/courses/4||To remove the course having course-id 4.|
To update student or course information:
|PUT||http://localhost/appName/student/10||For update the student record having ID number 10|
|PUT||http://localhost/appName/courses/4||For upgrading the course having course-id 4.|
To insert and create a new entry for the student:
|POST||http://localhost/appName/student||To create a new entry.|