How do you handle http request and response?

March 5,2021 - 9 min read
How do you handle http request and response?

Every website on the internet is majorly based on a two-way communication between clients and servers. Here by clients, we mean browsers like Chrome or Safari or any type of computing program or device whereas servers are often computers in the cloud.

This communication between the client and a server is done by a special mechanism of HTTP requests and responses. In this cycle, first a client sends an HTTP request to the web. Here the client is often a web browser. After the web server receives the request, it runs an application to process the request. After the processing, the server returns an HTTP response. This response is basically the output displayed on the browser; it receives this response from the web server. The request that we are talking about is in various formats, for example, HTML page, style sheet, Javascript code or just a simple text. The response will also be similar according to the request, for example, HTML file, CSS file, JPG file, JS file or data in XML or JSON format. This is popularly known as the HTTP response request circle.

In this blog we are going to look at how the HTTP requests and response are handled? We will understand the concept of HTTP Requests before we proceed to HTTP responses. Let’s begin!

What are HTTP Requests?

As mentioned above, if you want to access any resource from the vast array of them present on the internet and hosted on different servers, then your browser needs to send a request to the servers. If this request gets accepted, the browser will display the requested resources on your screen. The underlying format used for the effective communication between a client and a server is HTTP which is an acronym for Hypertext Transfer Protocol. In this cycle of communication, the message that a client sends is known as an HTTP request. These requests are sent with the help of various methods known as HTTP request methods. These methods indicate specific action that has to be performed on a given resource. There are some standard features shared by the various HTTP request methods. Let’s discuss the concept of HTTP Request Methods.

An HTTP request is an action performed on a resource identified by an URL. There are several request methods with each one of them with a specific purpose. These methods should always be written in upper case as they are case-sensitive.

The working of HTTP Requests is simple. As mentioned above, an HTTP request is sent by the server and is submitted to the server. The request is processed and then the server sends a response which contains the status information of the request. It is clear that HTTP requests work as an intermediary between a client or an application and a server.

It is now time we discuss the various types of HTTP Request Methods. The first and probably the most popular request method is the GET method. It retrieves and requests data from a particular computing resource present in a server. In simple terms, this popular HTTP request techniques is used to retrieve information that is identified by the URL.

Another HTTP request method is HEAD. This technique requests a reaction that is similar to that of GET request. However, the only difference is that the HEAD method doesn’t have a message-body in its response. This method is useful in recovering the meta-data without transferring the entire content. HEAD method is commonly used for testing hypertext links for accessibility, validity or recent modification.

POST is another popular HTTP request method. It is used to send data to a server in order to create or update a resource. The information submitted to the server is archived in the request body. One common use of the HTTP POST method is to send user-generated data to a server, for example, uploading a profile photo by a user.

One method similar to POST method is PUT. It is also used for sending data to the server to create or update a resource; however, the difference is that PUT requests are idempotent. Hence, if the same PUT requests are called multiple times then the results will always be the same.

We have covered the basic HTTP request methods generally used in server communication. There are a couple of more methods which we will discuss in the blog ahead. One such request method is DELETE which, as the name suggests, is used to delete resources indicated by the URL. Basically, a DELETE request removes the targeted resource.

A method similar to POST and PUT is the PATCH method. It is used for applying partial changes to the resource. It is also a non-idempotent method however it requires the user to only send the updated username and not the complete user entity.

A HTTP request method used to invoke an application loop-back test along the path to the target resource is the TRACE method. It allows clients to view messages that are being received on the other end of the request chain. This information is used for testing functions.

One last method that we are going to discuss is the CONNECT method which is used to create a network connection to a web server by the client. It basically creates a tunnel to the identifies server. One example of the CONNECT request method is SSL tunnelling.

We discussed the various methods or techniques for http REQUEST. In the segment below, we discussed some similarities and distinctions between various HTTP REQUEST methods.

HTTP PUT and POST Request Methods discussed above are both used to facilitate data transmission between a client and a server. They have similar roles in the response request cycle but there are some differences between them also. For example, the PUT request method is idempotent whereas POST request method is not. Another difference is that PUT request operates as specific but POST operates as abstract.

GET and POST methods can also be compared in order to understand them better. In GET method, the parameters are saved in the browser’s history whereas in the POST method, the parameters aren’t archived. Also, the GET method can be bookmarked but the same cannot be said for the POST method.

With this, we come to an end of our discussion on the concept of HTTP Request and the various methods associated with it. We also took a brief look over the comparison between some similar methods. Now, we will move on to our next section, where we will talk in detail about HTTP response and the concepts that are useful in the response cycle. Let’s begin!

What is HTTP Response?

We learnt in detail about HTTP request, the next step a request is generated involves HTTP Response. It is nothing but a packet containing information sent by the server to the client in response to the request made by the client. HTTP Response basically has the information requested by the Client. In this segment, we are going to discuss the structure of HTTP Response.

The structure of HTTP Response contains a status line, any number of headers or it can also be with no header, a request body in a status line is also optional.

A response status line consists of HTTP protocol version, status code and a reason phrase.

Let’s understand the structure of HTTP response with the help of an example.

We are taking an example where we will see the response section. The first line is called the Status Line. It is written in the following manner – Status Line: HHTP/1.1 200 OK

The status line has the HTTP Protocol Version mentioned as (HTTP/1.1). the next is Status Code defines as 200 and then the Status Message which in this case is OK.

The next section after the status line is the response header. It has zero or more header lines. Generally, the header lines are not zero. Response headers are placed after the status Line and before the Response Body. These headers pass some additional information to the Client.

The HTTP Response Header, for example, a header content-type, contains an application/json header with charset=utf-8. This example signifies that the body of the response will contain a JSON formatted data. This information is given to the client by the server. The response body should be interpreted it as a JSON by the client.

Next is the response body which has the resource data requested by the client. For example, a web development course was requested for the fees data, then the response body will contain the information of fees for the web development course. The response body is variable and can also have other information like syllabus, testimonial, duration and a few more properties of the particular course. The format of the text contained in HTTP Response Body is defined by the Response headers. In the explanation above, when we said resource, we referred to the information requested by a client. In this example, the details corresponding to a course is a resource.

With this, we come to an end of our discussion on the concept of HTTP Response and its structure as well as the request method and its methods.

If you wish to start your journey in web development, we have something very interesting for you. There is a professional web development course that will address all your concerns regarding web development and help you ace your skills. Make sure you check out the course that can be a stepping stone to your dream of becoming a proficient web developer.

One such course that we would like to recommend is Konfinity’s Web Development Course . This course is a well-researched training course developed by experts from IIT DELHI in collaboration with tech companies like Google, Amazon and Microsoft. It is trusted by students and graduates from IIT, DTU, NIT, Amity, DU and more.

We encourage technocrats like you to join the course to master the art of creating web applications by learning the latest technologies, right from basic HTML to advanced and dynamic websites, in just a span of a few months.

Konfinity is a great platform for launching a lucrative tech career. We will get you started by helping you get placed in a high paying job. One amazing thing about our course is that no prior coding experience is required to take up our courses. Start your free trial here .