The interaction between the client and the server propagates through the HTTP protocol. Moreover, we have already understood one end of this communication, i.e., the HTTP Requests in our previous article. Subsequently, we now need to understand the other end, which is the HTTP Response. Consequently, in this post, we will cover below points-
- What is HTTP Response?
- What is the structure of an HTTP Response?
- And, what are different HTTP Response Status Codes?
What is HTTP Response?
HTTP Response is the server's information as a result of the client's request. Additionally, it acts as an acknowledgment that the performance of the requested action is successful. In case there is an error in carrying out the client's request, the server responds with an error message. Moreover, the HTTP responses come as plain text formatted in either JSON or XML format, just like the HTTP requests. In the next section, let us see how an HTTP response looks.
What is the structure of an HTTP Response?
By looking at the various status codes above, we can understand the response from the server. However, the HTTP Response not only contains the response status code but some other components as well. We will discuss them in this section. The composition of HTTP Response looks like this-
- Status Line
- Zero or more headers
- An optional message body
Subsequently, let us understand these different components using the same API that we used in the HTTP Request article. Additionally, to reiterate, we will be using the below URL-
Once you hit the above URL in your browser window and open the Network tab in the developer tools parallelly, you will see some printed messages. Additionally, you may select Books as highlighted in the image below-
Consequently, let's move ahead and understand the components.
The status line in an HTTP response consists of 3 parts-
- HTTP Protocol Version
- Status Code
- Reason Phrase
It is the first line in the response section. In addition to that, below is the Status-Line for the response we received to our request.
HTTP Protocol Version
As already discussed in the previous post, the HTTP protocol version specifies the message format along with the sender's ability to understand further communication. Similar to the HTTP request, the HTTP protocol version in the response is 1.1.
Every HTTP response comes with a status code. Moreover, this status code is a 3-digit integer value. The first digit represents the class of the response. Moreover, the last 2 digits do not have any categorical value. There are five distinct values that the first digit of the status code can take. In our example, the status code is 200. But that is not the only status code your response may come with. Additionally, the below table summarizes the different status codes you might come across.
- 1xx: Informational: It means the request was received and the process is continuing.
- 22xx: Success: It means the action received was successful, understandable, and acceptable.
- 33xx: Redirection: This means further action must be taken to complete the request.
- 44xx: Client Error: It means the request contains incorrect syntax or cannot fulfill.
- 55xx: Server Error: It means the server failed to fulfill/ complete an apparently valid request.
Reason phrase gives/ provides a short textual description of the status code. It is like a status text that resonates with the status code. In our example, the reason phrase is OK.
A response header is similar to the request header, which consists of additional information about the response. Additionally, a response may have 0 or more headers, but it is highly unlikely to have zero response headers. Moreover, the response headers are present after the status line and before the response body. Consequently, in our example, the response header is as below-
Let's briefly discuss the different response headers-
- Server: describes the software of the origin server
- Content-Type: depicts the type of content the response holds. In our example, its value application/JSON; charset=utf-8 means that the response is in the form of JSON. Additionally, the client should interpret it in the same manner.
- Content-Length: defines the length of the data, i.e., the number of bytes in the response body.
The response body consists of the resource data requested by the client. In our example, we requested the book's data, and the response body consists of the different books present in the database along with their information.
As can be seen, the book list comes in response along with different book properties like its author, description, isbn, title, etc. Additionally, the message body helps the user with more information on the response, be it a valid response with data, some error in response, or the need for additional data from the client.
What are different HTTP Response Status Codes?
As seen above, Status codes separate into 5 different categories. Consequently, let's see those in details:
|1xx||Information, i.e., it denotes that the request has been received and under process.|
|100||Continue: The client can continue with the request as long as it doesn't get rejected.|
|101||Switching Protocols: The server is switching protocols.|
|2xx||Success, i.e., it denotes a successful receipt, processing, and acceptance of the request message.|
|200||OK: The request is OK.|
|201||Created: A successfully created new resource|
|202||Accepted: Request accepted for processing, but in progress|
|203||Non-Authoritative Information: The information in the entity header is not from an original source but a third-party|
|204||No Content: Response with status code and header but no response body|
|205||Reset Content: The form for the transaction should clear for additional input|
|206||Partial Content: Response with partial data as specified in Range header|
|3xx||Redirection, i.e., further action has to be taken for the request to complete|
|300||Multiple Choices: Response with a list for the user to select and go to a location|
|301||Moved Permanently: Requested page moved to a new url|
|302||Found: Requested page moved to a temporary new URL|
|303||See Other: One can find the Requested page under a different URL|
|305||Use Proxy: Requested URL need to access through the proxy mentioned in the Location header|
|307||Temporary Redirect: Requested page moved to a temporary new URL|
|4xx||Client Error, i.e., incorrect syntax or error in fulfillment of the request|
|400||Bad Request: Server unable to understand the request|
|401||Unauthorized: Requested content needs authentication credentials|
|403||Forbidden: Access is forbidden|
|404||Not Found: Server is unable to find the requested page|
|405||Method Not Allowed: Method in the request is not allowed|
|407||Proxy Authentication Required: Need to authenticate with a proxy server|
|408||Request Timeout: The request took a long time as expected by the server|
|409||Conflict: Error in completing request due to a conflict|
|411||Length Required: We require the "Content-Length" for the request to process|
|415||Unsupported Media Type: Unsupported media-type|
|5xx||Server Error, i.e., error invalid request fulfillment at server side|
|500||Internal Server Error: Request not completed due to server error|
|501||Not Implemented: Server doesn't support the functionality|
|502||Bad Gateway: Invalid response from an upstream server to the server. Hence, the request not complete|
|503||Service Unavailable: The server is temporarily down|
|504||Gateway Timeout: The gateway has timed out|
|505||HTTP Version Not Supported: Unsupported HTTP protocol version|
Subsequently, in the upcoming article, we will see some more concepts that will add to our base of learning RestAssured.
- To every client request, the server responds with an HTTP response.
- Moreover, we also understood the different HTTP response components along with their sub-components, like the HTTP Protocol version, status code, and reason phrase for the status line.
- Additionally, we saw that the status codes could start from only 5 distinct digits, and the server can send different status codes.