-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfetchAPI.js
More file actions
185 lines (129 loc) · 4.68 KB
/
fetchAPI.js
File metadata and controls
185 lines (129 loc) · 4.68 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// FETCH API IN JAVASCRIPT
// The Fetch API provides a modern way to make HTTP requests in JavaScript.
// It returns a Promise and is used for fetching resources like data from APIs.
// Fetch makes asynchronous requests simpler compared to the old XMLHttpRequest.
// It supports all HTTP methods (GET, POST, PUT, DELETE, etc.)
// BASIC SYNTAX
// fetch(url, options)
// .then(response => { ... })
// .catch(error => { ... });
//
// - url → The endpoint you're requesting.
// - options → (optional) configuration object (method, headers, body, etc.)
// EXAMPLE 1: SIMPLE GET REQUEST
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json()) // convert response to JSON
.then(data => console.log("All Posts:", data))
.catch(error => console.error(" Error:", error));
// WHAT HAPPENS HERE?
// 1. fetch() sends a request to the given URL.
// 2. The first .then() converts the response into a JavaScript object using response.json().
// 3. The second .then() gives access to the parsed data.
// 4. .catch() handles any network or parsing errors.
// EXAMPLE 2: MAKING A POST REQUEST (SENDING DATA)
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
title: 'New Post',
body: 'This is the body of the post',
userId: 1
})
})
.then(response => response.json())
.then(data => console.log("Created Post:", data))
.catch(error => console.error("Error:", error));
// WHY USE JSON.STRINGIFY()?
// The body of a POST request must be in string format when sending JSON data.
// JSON.stringify() converts a JS object into a JSON string.
// EXAMPLE 3: USING ASYNC / AWAIT
// A cleaner and more readable way to handle fetch requests.
async function getUsers() {
try {
console.log("Fetching users...");
const response = await fetch('https://jsonplaceholder.typicode.com/users');
if (!response.ok) {
throw new Error(`HTTP Error: ${response.status}`);
}
const users = await response.json();
console.log("Users fetched successfully:", users);
} catch (error) {
console.error(" Fetch failed:", error);
} finally {
console.log("Fetch attempt finished!");
}
}
getUsers();
// WHY USE ASYNC / AWAIT?
// - Makes code look synchronous and cleaner.
// - Easier to handle multiple async operations in sequence.
// - Works with try...catch for better error handling.
// EXAMPLE 4: PUT REQUEST (UPDATE DATA)
async function updatePost() {
try {
const response = await fetch('https://jsonplaceholder.typicode.com/posts/1', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title: 'Updated Title',
body: 'This content has been updated',
userId: 1
})
});
const updatedPost = await response.json();
console.log("Post Updated:", updatedPost);
} catch (error) {
console.error(" Error updating post:", error);
}
}
updatePost();
// EXAMPLE 5: DELETE REQUEST
async function deletePost() {
try {
const response = await fetch('https://jsonplaceholder.typicode.com/posts/1', {
method: 'DELETE'
});
if (response.ok) {
console.log("Post deleted successfully!");
} else {
console.error(" Failed to delete post!");
}
} catch (error) {
console.error(" Error:", error);
}
}
deletePost();
// EXAMPLE 6: FETCH WITH CUSTOM HEADERS (E.G. AUTH TOKEN)
fetch('https://api.example.com/data', {
headers: {
'Authorization': 'Bearer YOUR_ACCESS_TOKEN',
'Accept': 'application/json'
}
})
.then(response => response.json())
.then(data => console.log(" Authorized Data:", data))
.catch(error => console.error("Authorization Error:", error));
// EXAMPLE 7: HANDLING MULTIPLE FETCH REQUESTS
// Using Promise.all() to run requests in parallel.
const postsPromise = fetch('https://jsonplaceholder.typicode.com/posts').then(res => res.json());
const usersPromise = fetch('https://jsonplaceholder.typicode.com/users').then(res => res.json());
Promise.all([postsPromise, usersPromise])
.then(([posts, users]) => {
console.log(" Posts:", posts.length);
console.log("Users:", users.length);
})
.catch(error => console.error(" Error fetching data:", error));
// EXAMPLE 8: DISPLAYING DATA IN HTML (BASIC DEMO)
// You can use Fetch to populate HTML content dynamically.
async function displayPosts() {
const response = await fetch('https://jsonplaceholder.typicode.com/posts');
const posts = await response.json();
// Create a simple list in the console (or DOM)
console.log(" Displaying first 5 posts:");
posts.slice(0, 5).forEach(post => {
console.log(` ${post.title}`);
});
}
displayPosts();