|
| 1 | +[](/chapters/ch06.0-http-deep-dive.md) |
| 2 | + |
| 3 | +## HTTP Verbs |
| 4 | + |
| 5 | +In the previous chapter, we made a simple request to `http://localhost:3000` with `cURL` and received the following output on the terminal: |
| 6 | + |
| 7 | +```bash |
| 8 | +$ curl http://localhost:3000 -v |
| 9 | +* Trying 127.0.0.1:3000... |
| 10 | +* Connected to localhost (127.0.0.1) port 3000 (#0) |
| 11 | +> GET / HTTP/1.1 |
| 12 | +> Host: localhost:3000 |
| 13 | +> User-Agent: curl/7.87.0 |
| 14 | +> Accept: */* |
| 15 | +> |
| 16 | +* Mark bundle as not supporting multiuse |
| 17 | +< HTTP/1.1 200 OK |
| 18 | +< Date: Wed, 23 Aug 2023 13:13:32 GMT |
| 19 | +< Connection: keep-alive |
| 20 | +< Keep-Alive: timeout=5 |
| 21 | +< Content-Length: 11 |
| 22 | +< |
| 23 | +* Connection #0 to host localhost left intact |
| 24 | +Hello world% |
| 25 | +``` |
| 26 | +
|
| 27 | +Our focus of this chapter will be the first line of the request: |
| 28 | +
|
| 29 | +```bash |
| 30 | +> GET / HTTP/1.1 |
| 31 | +``` |
| 32 | +
|
| 33 | +This starts with `GET` which is a **HTTP method** or commonly called as HTTP verb, because they describe the action. These HTTP methods are an essential part of the communication process between client applications, such as web browsers, and web servers. They provide a structured and standardized way for client applications to interact with web servers, ensuring that communication is clear and concise. |
| 34 | +
|
| 35 | +HTTP methods offer a set of instructions for the server to carry out, which can include retrieving a resource, submitting data, or deleting a resource. These methods are essential for ensuring that client applications can perform a variety of tasks in a streamlined and efficient way. |
| 36 | +
|
| 37 | +The most commonly used HTTP methods include `GET`, `POST`, `PUT`, `DELETE`, and `PATCH`, each with a specific purpose and set of rules. For example, the `GET` method is used to retrieve data from a server, while the `POST` method is used to submit data to a server for processing. By following these rules, client applications can communicate effectively with web servers, ensuring that data is transmitted correctly and that the server can respond appropriately. |
| 38 | +
|
| 39 | +### `GET` - Retrieve data |
| 40 | +
|
| 41 | +The most basic example of the `GET` method is typing any URL in the browser and pressing enter. You're looking to `GET` the contents of a certain website. |
| 42 | +
|
| 43 | +Then, the browser sends a request to the server that hosts the website, asking for the content you want to see. The server processes the request and sends a response back to your browser with the content you requested. |
| 44 | +
|
| 45 | +The `GET` method helps digital content (like text, pictures, HTML pages, stylesheets and sounds) appear on your web browser. |
| 46 | +
|
| 47 | +When a client sends a GET request to a server, the server should only return data to the client and not modify or change its state. This means that the server should not alter any data on the server-side or perform any actions that could modify the system's state in any way. |
| 48 | +
|
| 49 | +This is important to ensure that the data requested by the client is accurate and consistent with the server-side data. By following this protocol, developers can ensure that their applications function smoothly and without any unexpected side effects that could cause problems in the future. |
| 50 | +
|
| 51 | +> Note: It's technically possible to modify anything on a GET request from the server side, but it's a bad practice and goes against standard HTTP protocol rules. Stick to the REST API guidelines for good API design. |
| 52 | +
|
| 53 | +### `POST` - Create something |
| 54 | +
|
| 55 | +The `POST` method serves a different purpose compared to the `GET` method. While `GET` is used for retrieving data, `POST` is employed when you want to send data to a server, to create a new resource on the server. |
| 56 | +
|
| 57 | +Imagine you're filling out a form on a website to create a new account. When you submit the form, the website sends a `POST` HTTP request to send the information you provided (such as your username, password, and email) to the server. The server processes this data, typically validating it, and if everything is fine - create a new user account. |
| 58 | +
|
| 59 | +Just like with `GET`, it's crucial to follow proper HTTP API guidelines when using the `POST` method. Ensuring that your `POST` requests only create data and don't have unintended side effects helps maintain the integrity of your application and the server's data. |
| 60 | +
|
| 61 | +> Note: Again, it is technically possible to use the `POST` method to retrieve data from a server, or update data, it's considered non-standard and goes against conventional HTTP practices. |
| 62 | +
|
| 63 | +### `PUT` - Replace or Create |
| 64 | +
|
| 65 | +The PUT method is used to change or replace data that already exists on the server, completely. It's important to remember that, according to HTTP API guidelines, the PUT request should not be used to update only part of the content. |
| 66 | +
|
| 67 | +You may ask, aren't `POST` and `PUT` same as they do the same thing - `CREATE` data if it doesn't exist? There's a difference. |
| 68 | +
|
| 69 | +The main difference between the `PUT` and `POST` methods is that PUT is considered **idempotent**. This means that calling it once or multiple times in a row will result in the same outcome, without any additional side effects. |
| 70 | +
|
| 71 | +In contrast, successive identical [POST](<https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST>) requests may have additional effects, such as creating multiple instances of a resource or submitting duplicate orders. |
| 72 | +
|
| 73 | +### `HEAD` - Retrieve Metadata |
| 74 | +
|
| 75 | +The `HEAD` method shares a resemblance to the `GET` method in that it retrieves information from the server, but with one fundamental difference: it only retrieves the metadata associated with a resource, not the resource's actual content. |
| 76 | +
|
| 77 | +Metadata can include details like the resource's size, modification timestamp, content-type, and more. |
| 78 | +
|
| 79 | +When a `HEAD` request is made to a server, the server processes the request in a manner similar to a `GET` request, but instead of sending back the full content, it responds with just the metadata in the HTTP headers. This can be useful when a client needs information about a resource without the need to transfer the entire content, which can save bandwidth and improve performance. |
| 80 | +
|
| 81 | +For example, imagine a web page that lists links to various files for download. When a user clicks on a link, the client can initiate a `HEAD` request to gather information about the file, such as its size, without actually downloading the file itself. |
| 82 | +
|
| 83 | +By using the `HEAD` method when needed, we can optimize data retrieval, minimize unnecessary network traffic, and obtain crucial resource information efficiently and quickly. |
| 84 | +
|
| 85 | +> It's worth noting that servers are not required to support the `HEAD` method, but when they do, they should ensure that the metadata provided in the response headers accurately reflects the current state of the resource. |
| 86 | +
|
| 87 | +### `DELETE` - Remove from existence |
| 88 | +
|
| 89 | +The `DELETE` method is used when you need to remove or delete a specific resource from the server. When using the `DELETE` method, it's important to know that the action is **idempotent**. This means that no matter how many times you execute the same `DELETE` request, the outcome remains the same – the targeted resource is removed. |
| 90 | +
|
| 91 | +We should be careful when using the DELETE method, as it's purpose is to permanently remove the resource from the server. To avoid accidental or unauthorized deletions, it's recommended to use proper authentication and authorization mechanisms. |
| 92 | +
|
| 93 | +After successfully deleting a resource, the server may respond with a `204 No Content` status code to indicate that the resource has been removed, or a 404 Not Found status code if the resource was not found on the server. |
| 94 | +
|
| 95 | +If you only need to remove part of something and not the whole thing, using the `DELETE` method might not be the best idea. To make things clear and well-organized, it's better to identify each part as a separate resource, like with the `PATCH` method for partial updates. |
| 96 | +
|
| 97 | +### `PATCH` - Partial updates |
| 98 | +
|
| 99 | +The `PATCH` method is used to partially update an existing resource on the server. `PATCH` is different from the `PUT` method, which replaces the entire resource. With `PATCH`, you can modify only the specific parts of the resource's representation that you want to change. |
| 100 | +
|
| 101 | +So, if you're looking to update a document, or update a user, use the `PATCH` method. |
| 102 | +
|
| 103 | +PATCH requests provide instructions to the server on how to modify a resource. These instructions may include adding, modifying, or removing fields or attributes. The server executes the instructions and makes the changes to the resource accordingly. |
| 104 | +
|
| 105 | +### A small recap |
| 106 | +
|
| 107 | +| Method Name | Description | |
| 108 | +| ----------- | ---------------------------------------------------------------------------------------------------------------- | |
| 109 | +| GET | Transfers a current representation of the target resource to the client. | |
| 110 | +| HEAD | Same as GET, but doesn't transfer the response content - only the metadata. | |
| 111 | +| POST | Creates new data. | |
| 112 | +| PUT | Replaces the current representation of the target resource with the request content, if not found - creates one. | |
| 113 | +| DELETE | Removes all current representations of the target resource. | |
| 114 | +| PATCH | Modifies/Updates data partially. | |
| 115 | +
|
| 116 | +There are mother HTTP methods as well, but these are the most commonly used ones, and would suffice for most use-cases. |
| 117 | +
|
| 118 | +In the next chapter, we'll take a look at status codes. |
| 119 | +
|
| 120 | + |
0 commit comments