Types of HTTP Request Methods
There is a wide array of resources on the internet, hosted on servers. To utilize these resources, a browser or a client program sends a request to the respective server. Most browsers access the World Wide Web which uses the HTTP protocol. Every message or request or response, communicated over the World Wide Web are essentially HTTP Requests. However, there are certain exceptions.
HTTP defines certain request methods that indicates the action to be performed. They are also referred to as HTTP verbs.
Have a look at the screenshot below:
Take a look at the URL of this video. After the name of the website (i.e., youtube.com), we can see something like a query. The URL doesn’t only specifies which website, it also contains the information about which video and a time stamp in that video.
The term ‘watch’ is used as a token for what kind of result we want (in this case a video that we want to watch), and after the question mark, there are two parameters separated by a logical ‘&’. The first part “v=24NrLl8EhDM”, acts as a video ID where as the second part “t=600s”, acts as a time stamp, i.e., if opened with this query attached, the video will open from 600 seconds (or 10 minutes).
This type of a URL is created when an HTTP request uses GET method. Various methods are used to denote what kind of action has to be performed. GET method is used to retrieve something from the server. As for our example, we are retrieving a video with that particular Video ID, opened to that particular time stamp.
A list of the HTTP request methods :
GET
To retrieve information from the given server using a given URL.
HEAD
Same as GET, but transfers the status line and header section only.
POST
To send some data to the server.
PUT
To store some data at a location specified by the given URL.
DELETE
To delete some file at the server end.
CONNECT
To establish a network with the server.
OPTIONS
To find out what methods are supported by the server.
TRACE
To echo the contents of the HTTP Request back to the requester that can be used for debugging.
Let us see where these methods are specified by looking at a Java Program.
public class RequestHandler implements HttpHandler {
@Override
public void handle(HttpExchange httpExchange) throws IOException{
String method = httpExchange.getRequestMethod();
try {
if (method.equals(GET_METHOD)) {
//Code corresponding to GET method } else if (method.equals(POST_METHOD)) {
//Code corresponding to POST method } else if (method.equals(PUT_METHOD)) {
//Code corresponding to PUT method }
}catch (Exception e){
}
}
}
The above method is an over-ridden method in the class that implements the interface HttpHandler . The method that is used can be simply extracted using the getRequestMethod() method of the HttpExchange class. To see the tutorial to create a Java Server, check my post “Develop an HTTP Server in Java”.
The following is an example of a server that can only handle GET and POST requests.
For the purpose of this tutorial, we store our data in a HashMap. However, in most original real world servers, data is retrieved from a database.
Here is a working demo for the above code :