Skip to content

Commit 414add9

Browse files
committed
add the chapter 6.1 - HTTP verbs
1 parent 6bab742 commit 414add9

File tree

3 files changed

+146
-16
lines changed

3 files changed

+146
-16
lines changed

Readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,13 @@ The repo for our backend framework- [Velocy](https://github.com/ishtms/velocy)
161161
- [Starting our web server](/chapters/ch06.0-http-deep-dive.md#starting-our-web-server)
162162
- [Testing our web server](/chapters/ch06.0-http-deep-dive.md#testing-our-web-server)
163163
- [Testing with `cURL`](/chapters/ch06.0-http-deep-dive.md#testing-with-curl)
164+
- [HTTP Verbs](/chapters/ch06.1-http-verbs.md)
165+
- [`GET` - Retrieve data](/chapters/ch06.1-http-verbs.md#get---retrieve-data)
166+
- [`POST` - Create something](/chapters/ch06.1-http-verbs.md#post---create-something)
167+
- [`PUT` - Replace or create](/chapters/ch06.1-http-verbs.md#put---replace-or-create)
168+
- [`HEAD` - Retrieve metadata](/chapters/ch06.1-http-verbs.md#head---retrieve-metadata)
169+
- [`DELETE` - Remove from existence](/chapters/ch06.1-http-verbs.md#delete---remove-from-existence)
170+
- [`PATCH` - Partial updates](/chapters/ch06.1-http-verbs.md#patch---partial-updates)
171+
- [A small recap](/chapters/ch06.1-http-verbs.md#a-small-recap)
164172

165173
![](https://uddrapi.com/api/img?page=readme)

chapters/ch06.0-http-deep-dive.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,106 +155,108 @@ That's a lot of stuff to digest. One thing to notice is two different operators
155155
156156
Let me walk through this line by line:
157157
158-
```
158+
```bash
159159
Trying 127.0.0.1:3000...
160160
```
161161
162162
This line is indicating that `cURL` is attempting to establish a connection to the IP address 127.0.0.1. This IP address is also known as localhost and it refers to the local computer you are currently working on.
163163
164164
The connection is being made on port number 3000, which is a number used to identify a specific process to which data is being sent or received. We'll talk about PORTs in the next chapter.
165165
166-
```
166+
```bash
167167
Connected to localhost (127.0.0.1) port 3000 (#0)
168168
```
169169
170170
This means the connection to the specified IP address and port has been successfully established. This is great news and means that you can proceed with your task without any further delay.
171171
172172
The (#0) in the message refers to the connection index. This is helpful information, especially if you're making multiple connections at the same time. By keeping track of the connection index, you can avoid any confusion or errors that could arise from mixing up different connections.
173173
174-
```
174+
```bash
175175
> GET / HTTP/1.1
176176
```
177177
178178
**`cURL`** is sending an HTTP request to the server using the HTTP method **`GET`**. The **`/`** after the method indicates that the request is being made to the root path, or the root endpoint of the server.
179179
180-
```
180+
```bash
181181
> Host: localhost:3000
182182
```
183183
184184
This line specifies that the `cURL` command is setting the **`Host`** header on the **request**, which tells the server the domain name and port of the request.
185185
186-
```
186+
```bash
187187
> User-Agent: curl/7.87.0
188188
```
189189
190190
This line is also part of the HTTP request headers. It includes the **`User-Agent`** header, which identifies the client making the request. In this case, it indicates that the request is being made by **`curl`** version 7.87.0.
191191
192-
```
192+
```bash
193193
> Accept: */*
194194
```
195195
196196
This line sets the **`Accept`** header, which tells the server what types of response content the client can handle. In this case, it indicates that the client accepts any type of content.
197197
198-
```
198+
```bash
199199
>
200200
```
201201
202202
This line indicates the end of the HTTP request headers. An empty line like this separates the headers from the request body, which is not present in this case because it's a `GET` request. We'll talk about `POST` and other http verbs/methods in the upcoming chapters.
203203
204-
```
204+
```bash
205205
Mark bundle as not supporting multiuse
206206
```
207207
208208
This is an internal log message from **`curl`** and doesn't have a direct impact on the request or response interpretation. It's related to how **`curl`** manages multiple connections in a session.
209209
210-
```
210+
```bash
211211
< HTTP/1.1 200 OK
212212
```
213213
214214
This line is part of the HTTP response. It indicates that the server has responded with an HTTP status code **`200 OK`**, which means the request was successful. Again, we're going to understand HTTP status codes in the next chapter. For now, it's enough to think that any status code in the form `2xx`, where x is a number, means everything is fine.
215215
216-
```
216+
```bash
217217
< Date: Wed, 23 Aug 2023 13:13:32 GMT
218218
```
219219
220220
This line is part of the HTTP response headers. Important thing to note, this is the header set by the server, and not the client. It includes the **`Date`** header, which indicates the date and time when the response was generated on the server.
221221
222-
```
222+
```bash
223223
< Connection: keep-alive
224224
```
225225
226226
This line is part of the response headers and informs the client that the server wants to maintain a persistent connection, and re-use the connection for potential future requests. This helps with the performance.
227227
228-
```
228+
```bash
229229
< Keep-Alive: timeout=5
230230
```
231231
232232
This line, also part of the response headers, specifies the duration of time (5 seconds in this case) that the server will keep the connection alive if no further requests are made.
233233
234-
```
234+
```bash
235235
< Content-Length: 11
236236
```
237237
238238
This line indicates the length of the response content in bytes. In this case, the response body has a length of 11 bytes.
239239
240-
```
240+
```bash
241241
<
242242
```
243243
244244
This line marks the end of the response headers and the beginning of the response body.
245245
246-
```
246+
```bash
247247
Connection #0 to host localhost left intact
248248
```
249249
250250
This line is a log message from **`curl`** indicating that the connection to the server is being left open (**`intact`**) and not closed immediately after receiving the response. It could potentially be reused for subsequent requests.
251251
252-
```
252+
```bash
253253
Hello world%
254254
```
255255
256256
This is the actual response body returned by the server. In this case, it's a simple text string saying "Hello world". The `%` is nothing to worry about. It indicates that the response doesn't ends with a `\n` character.
257257
258258
Now that we understand what are the basic components of the `request` and the `response`, let's understand these components in more detail, in the next chapter
259259
260+
[![Read Prev](/assets/imgs/next.png)](/chapters/ch06.1-http-verbs.md)
261+
260262
![](https://uddrapi.com/api/img?page=http_deep_dive)

chapters/ch06.1-http-verbs.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
[![Read Prev](/assets/imgs/prev.png)](/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+
![](https://uddrapi.com/api/img?page=http_verbs)

0 commit comments

Comments
 (0)