-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDifference between GET & HEAD HTTP Requests
71 lines (55 loc) · 3.32 KB
/
Difference between GET & HEAD HTTP Requests
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
Lets learn a very important difference in API : GET vs HEAD
*****************
🔴 GET Method
*****************
✅ Usage: The GET method is used to request data from the server. It is meant for retrieving resources identified by the given URL. GET requests should only retrieve data and
should not have any other side effects on the server.
✅ Example Scenario: When you open a web page in your browser, the browser sends a GET request to the server to fetch the HTML, CSS, JavaScript, and other assets
required to render the web page.
✅ REST Assured Example:
given()
.when()
.get("/api/users/123")
.then()
.statusCode(200)
.body("key", equalTo("value"));
✅ Response: The server processes the request and returns the requested data in the response body along with metadata (such as headers and status codes).
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": 123,
"username": "john_doe",
"email": "[email protected]",
"status": "active"
}
1. The GET request method is used to request data from a specified resource, typically a web server.
2. It is a safe and idempotent operation, which means it should not change the server's state or have any side effects.
3. GET requests are typically used for retrieving data, such as web pages, images, documents, or any resource that doesn't require data to be sent to the server.
4. Parameters are usually included in the URL's query string, e.g., https://example.com/api/resource?id=123.
5. GET requests can be cached by browsers and intermediaries, making them suitable for read-only operations.
6. They are widely used for fetching web pages and data from APIs.
******************
🔴 HEAD Method
******************
✅ Usage: The HEAD method is very similar to GET but is used when you only want to retrieve the headers of the response without the response body. It is useful when you
need to check if a resource exists, retrieve metadata, or check the resource's modification date without actually downloading the content.
✅ Example Scenario: When you want to check if a specific file exists on a web server without downloading the entire file, you can use the HEAD method.
✅ REST Assured Example:
given()
.when()
.head("/api/users/123")
.then()
.statusCode(200)
.header("Content-Type", "application/json");
✅ Response: The server processes the request but only returns the headers (metadata) in the response, not the actual data.
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 548
Last-Modified: Tue, 10 Aug 2023 15:30:00 GMT
1. The HEAD request method is similar to GET, but it is used to request the headers of a resource without retrieving the resource's body.
2. It is useful when you want to check the status or metadata of a resource, such as its content type or last modification date, without actually downloading the resource.
3. Like GET, HEAD requests are also safe and idempotent, meaning they should not change the server's state.
4. They are often used to check the freshness of cached resources or to determine if a resource exists.
In summary, GET is used for retrieving data along with the response body, while HEAD is used for retrieving metadata and headers without the response body.
IMP: Both methods are useful in different scenarios, and the choice between them depends on your specific requirements, such as whether you need the actual content
or just metadata.