You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: curriculum/challenges/english/blocks/lecture-understanding-asynchronous-programming/673407be6af21d6766ed4b96.md
In the previous lesson, we saw what the Fetch API is and how to use it. In this lesson, we will discuss about the `GET`, `POST`, `PUT` and `DELETE` HTTP methods of Fetch API.
10
+
In the previous lesson, we saw what the Fetch API is and how to use it. In this lesson, we will discuss the `GET`, `POST`, `PUT`, and `DELETE` HTTP methods of Fetch API.
11
11
12
12
Let's start with the most common HTTP method which is the `GET` method. This is used to retrieve data from a server. When you use `fetch()` without specifying a method, it defaults to `GET`.
13
13
14
14
```js
15
15
fetch('https://api.example.com/data')
16
16
```
17
17
18
-
In this code, we're making a `GET` request to `https://api.example.com/data`. Now, please note that you cannot use this data directly, you have to convert the response to a JSON format. Only then you can use it anywhere you want in your project. Here’s an example of how to do it:
18
+
In this code, we're making a `GET` request to `https://api.example.com/data`. Now, please note that you cannot use this data directly; you have to convert the response to a JSON format. Only then you can use it anywhere you want in your project. Here’s an example of how to do it:
19
19
20
20
```js
21
21
fetch('https://api.example.com/data')
@@ -27,7 +27,7 @@ In this code, the response coming from the Fetch API is a promise, and we are us
27
27
28
28
The value of a promise is not known when the promise is created. It’s only known when the asynchronous process is completed. When we chain the two `.then` handlers to the fetch call, this is something called promise chaining which will be taught in the next lesson.
29
29
30
-
So far we have been retrieving resources from a server. But, did you know that we can also send data to the server? The `POST` method is used to send data to a server to create a resource. Here’s an example of a `POST` method which is used to create data into the server:
30
+
So far we have been retrieving resources from a server. But, did you know that we can also send data to the server? The `POST` method is used to send data to a server to create a resource. Here’s an example of a `POST` method which is used to create a resource on the server:
In this example, look carefully at the URL, you can see a `45` at the end. This is typically used as a unique ID to identify the data we are trying to update. We used the `PUT` method on the code and also specified the data as the `body` which will be used to update the identified data.
62
+
In this example, look carefully at the URL, you can see a `45` at the end. This is typically used as a unique ID to identify the data we are trying to update. We used the `PUT` method in the code and also specified the data as the `body` which will be used to update the identified data.
63
63
64
64
The `DELETE` method is used to delete a resource from the server. Here’s an example:
0 commit comments