HTTP and REST APIs
By: Rishav Bharti
Tags: Rest Api, Http Request

HTTP is the most popular application protocol on the internet, which allows transfer of data between machines and makes actions like visiting web pages happen.

When we enter a website URL, the browser creates a HTTP Request on our behalf and sends it to the server on which the website is hosted.

The server machine receives the request, understands it and takes appropriate action. The response again has to be formatted in a specific manner adhering to the HTTP protocol for the client to make sense of it.

And depending upon whether it’s a success, sends a HTML document along with the other resources like images, CSS & JavaScript which is read by the browser and rendered beautifully as web pages.

To better understand how a request message looks like, press Ctrl+Shift+I . This will open the Developer tools on your screen. Now, click on Network tab if it isn’t already open and reload the page. When it’s done reloading, click on any of the item in the list and it will open the Headers tab.

For better understanding, I’m attaching an image for reference below.

Now, two important thing to understand here is about Request Method and Status Code.

HTTP Request Methods

HTTP protocol defines a set of request methods. A client can use one of these request methods to send a request message to an HTTP server. The methods are:

GET : A client can use the GET request to get a web resource from the server.

: A client can use the GET request to get a web resource from the server. HEAD : A client can use the HEAD request to get the header that a GET request would have obtained. Since the header contains the last-modified date of the data, this can be used to check against the local cache copy.

: A client can use the HEAD request to get the header that a GET request would have obtained. Since the header contains the last-modified date of the data, this can be used to check against the local cache copy. POST : Used to post data up to the web server.

: Used to post data up to the web server. PUT : Used to already update an already existing data.

: Used to already update an already existing data. DELETE : Ask the server to delete the data.

: Ask the server to delete the data. TRACE : Ask the server to return a diagnostic trace of the actions it takes.

: Ask the server to return a diagnostic trace of the actions it takes. OPTIONS : Ask the server to return the list of request methods it supports.

: Ask the server to return the list of request methods it supports. CONNECT : Used to tell a proxy to make a connection to another host and simply reply the content, without attempting to parse or cache it. This is often used to make SSL connection through the proxy.

: Used to tell a proxy to make a connection to another host and simply reply the content, without attempting to parse or cache it. This is often used to make SSL connection through the proxy. Other extension methods.

HTTP Status Codes

HTTP Status codes are part of the HTTP Response. It helps the client understand what happened to the request. Status codes are 3 digit numbers (201, 304, etc.) and are categorized to 5 different families based on their starting digit. Along with the status code, a Reason-Phrase is also present (OK, Moved Permanently etc.) which gives a short description of the status code. The Status Code is intended for machines whereas Reason-Phrase is for humans.

1xx — Informational responses

2xx — Successful responses eg: 200 OK — Request successfully completed

3xx — Redirects eg: 301 Moved Permanently — Requested resource was moved permanently to a different location

4xx — Client errors eg: 404 Not Found — Requested resources wasn’t found

5xx — Server errors

That’s it, now you know the basics of HTTP. Here are the important parts: