State the core components of an HTTP response ?
Have you ever thought about how the front-end of an application communicates with the backend to get data or perform certain operations? It is done through API Requests. API stands for Application Programming Interface. The communication between our client and the API is achieved using HTTP Request which is followed by a Response to the client. Both the Requests and Response follows a certain syntax and structure to ease the communication process.
Whenever our client application wants to communicate to the server, it sends out a message to the server using HTTP Protocols, which is also termed as the HTTP Request. Based on that message, the server performs certain operations as demanded by the message and then replies to the client through a message, also knows as HTTP Response.
Below is an image depicting the Request-Response Cycle:
Structure of HTTP Response: As discussed above, the HTTP Response has a special structure that is followed so that the client can easily understand it. There exists a Universal Language that everybody follows so that there is no communication gap between people. HTTP Response broadly has 3 main components:
- Status Line
- Body (Optional)
An HTTP Response as a whole looks like the below picture:
Let us go through each of them one by one:
Status Line: An example of a Status-Line is given below:
HTTP/1.1 200 OK
The Status Line contains three important components – HTTP Version, HTTP Response Code, and a Reason-Phrase.
- HTTP Version: The HTTP version number shows the HTTP specification to which the server has tried to make the response message comply. In the above example, 1.1 is the HTTP Version.
- HTTP Response Code: It is a 3 digit number that shows the conclusion of the Request. In the above example, the response code 200 denotes that the content requested was OK. A very popular Status Code that we frequently encounter is 404 which represents the requested resource was not found.
- Reason-Phrase: Also known as Status Text as it summarizes the Status Code in human-readable form.
Response Header: The Response Header contains the information about the content that is being returned in response together with data about the Server that sent it. This information helps the Client/Browser in deciding in what way the response data would be used. In other words, headers can be said as metadata that is sent together with a response to provide more info about it.
The Server can send as many headers as needed. The headers are sent as a key-value pair separated by a colon ( : ). Although the server can send as many headers as required, the most popular response headers are Content-Length, Content-Type, Date, Server, Set-Cookie, etc.
Date: Thu, 16 Jan 2016 08:16:18 GMT Server: IBM_CICS_Transaction_Server/3.1.0(zOS) Content-type: image/jpg
In the above example, the response header shows the date and time when the response was sent, the server that sent the response, and the type of content that was sent, which here is a jpg image file.
Body: In case of a successful response, the body of the Response Message is used to serve the Client/User with the resource asked for in the request. Although the body is optional, it is one of the most fundamental parts of the communication between the Client and Server and is sent most of the time. The body carries the data and can be in one of the many formats such as json, html, image, etc. which is accordingly specified in the headers.
In case of some errors, the body might provide the reason for the errors or the actions needed to complete the request successfully. Sometimes, it may have a link to guide the user to some other page.