Breaking Down HTTP Protocol : A Step-by-Step Guide for API Testers

RamKrishna
3 min readMar 14, 2023

--

As a QA Automation Engineer, understanding the HTTP protocol is crucial for testing web applications and APIs. In this article, we will go over the basics of the HTTP protocol, including HTTP requests and responses, HTTP methods and headers, and HTTP status codes. We will also provide examples and code snippets to help illustrate these concepts.

HTTP Request and Response:

HTTP (Hypertext Transfer Protocol) is a protocol that enables communication between web clients and servers. When a client makes a request to a server, it sends an HTTP request message to the server. The server then responds with an HTTP response message.

HTTP Request:

An HTTP request message consists of several parts:

  1. Request Line: The request line contains the HTTP method, the resource being requested, and the HTTP version.
  2. Headers: Headers provide additional information about the request, such as the content type, user agent, and cookies.
  3. Body: The body contains any additional data that is sent with the request, such as form data or JSON data.

Here is an example of an HTTP request:

POST /api/login HTTP/1.1
Host: www.example.com
Content-Type: application/json
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Content-Length: 35

{"username":"johndoe","password":"secret"}

HTTP Response:

An HTTP response message also consists of several parts:

  1. Status Line: The status line contains the HTTP version, status code, and status message.
  2. Headers: Headers provide additional information about the response, such as the content type, server type, and cookies.
  3. Body: The body contains the response data, such as HTML or JSON data.

Here is an example of an HTTP response:

HTTP/1.1 200 OK
Content-Type: application/json
Server: Apache/2.4.18 (Ubuntu)
Set-Cookie: sessionId=12345; Expires=Wed, 09 Jun 2021 10:18:14 GMT
Content-Length: 52

{"message": "Login successful", "token": "abcdefg12345"}

HTTP Methods and Headers:

HTTP methods, also known as HTTP verbs, are used to indicate the type of action that the client wants to perform on the resource. The most common HTTP methods are:

  1. GET: Retrieves a resource from the server.
  2. POST: Creates a new resource on the server.
  3. PUT: Updates an existing resource on the server.
  4. DELETE: Deletes a resource from the server.

HTTP headers are used to provide additional information about the request or response. Some common HTTP headers include:

  1. Content-Type: Indicates the type of content that is being sent or received.
  2. User-Agent: Indicates the browser or client making the request.
  3. Authorization: Indicates the authentication credentials for the request.
  4. Cache-Control: Indicates how the client should cache the response.

Here is an example of an HTTP request using the GET method and the Accept header:

GET /api/products HTTP/1.1
Host: www.example.com
Accept: application/json

And here is an example of an HTTP response with the Content-Type header:

HTTP/1.1 200 OK
Content-Type: application/json
Server: Apache/2.4.18 (Ubuntu)
Content-Length: 58

{"products": [{"id": 1, "name": "Product 1", "price": 10.99}]}

HTTP Status Codes:

HTTP status codes are used to indicate the status of the request or response.

There are several status codes that are commonly used in HTTP. Some of the most common status codes include:

  1. 200 OK: The request was successful, and the server returned the requested data.
  2. 201 Created: The request was successful, and a new resource was created on the server.
  3. 400 Bad Request: The request was malformed or invalid, and the server could not process it.
  4. 401 Unauthorized: The request requires authentication, and the client has not provided valid credentials.
  5. 403 Forbidden: The client does not have permission to access the requested resource.
  6. 404 Not Found: The requested resource was not found on the server.
  7. 500 Internal Server Error: An error occurred on the server while processing the request.
  8. 503 Service Unavailable: This status code indicates that the server is currently unavailable to handle the request.

Here is an example of an HTTP response with a 404 status code:

HTTP/1.1 404 Not Found
Content-Type: text/html
Server: Apache/2.4.18 (Ubuntu)
Content-Length: 127

<!DOCTYPE html>
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>404 Not Found</h1>
<p>The requested resource could not be found.</p>
</body>
</html>

Conclusion:

Understanding the basics of the HTTP protocol is essential for any QA Automation Engineer working with web applications and APIs. In this article, we have gone over the HTTP request and response, HTTP methods and headers, and HTTP status codes.

We have also provided examples and code snippets to help illustrate these concepts. By mastering these fundamental concepts, you will be well on your way to becoming a proficient QA Automation Engineer.

--

--

RamKrishna
RamKrishna

Written by RamKrishna

Tech Enthusiast, SDET | Test Automation Consultant. I write about QA Trends, API Testing, and Test Automation.

No responses yet