-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFetchAPI.js
145 lines (131 loc) · 3.09 KB
/
FetchAPI.js
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
try {
const data = fetch('https://jsonplaceholder.typicode.com/todos/1')
console.log(data)
data.then((data) => {
console.log("Data: ",data)
return data.json()
})
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
throw err
})
}
catch(err){
console.log("Error: ",err);
}
// async function FetchApi(){
// try {
// const data = await fetch("https://jsonplaceholder.typicode.com/todos/4")
// console.log(await data.json())
// }
// catch (err){
// console.log(err)
// }
// }
//
// FetchApi()
// const newuser={
// name:"Sarthik",
// job:"Intership"
// }
// async function FetchApi(){
// try {
// const res = await fetch("https://reqres.in/api/users",{
// method:"POST",
// headers:{
// 'Content-Type':'application/json'
// },
// body:JSON.stringify(newuser)
// })
// const data = await res.json();
// if(!res.ok){
// console.log(data.description)
// return;
// }
// console.log("Add Data: ",data)
// }
// catch (err){
// console.log(err)
// }
// }
// FetchApi();
// async function FetchApi1(){
// try {
// const res = await fetch("https://reqres.in/api/users/39",{
// method:"PUT",
// headers:{
// 'Content-Type':'application/json'
// },
// body:JSON.stringify({
// name:"xyz"
// })
// })
// const data = await res.json();
// if(!res.ok){
// console.log(data.description)
// return;
// }
// console.log("Update Data",data)
// }
// catch (err){
// console.log(err)
// }
// }
// FetchApi1();
// async function FetchApi2(){
// try {
// const res = await fetch("https://reqres.in/api/users/3", {method:"DELETE"})
// const data = await res.json();
// if(!res.ok){
// console.log(data.description)
// return;
// }
// console.log("delete data",data)
// }
// catch (err){
// console.log(err)
// }
// }
// FetchApi2();
const newuser = {
name: "Sarthik",
email: "[email protected]"
}
url = 'https://jsonplaceholder.typicode.com/users'
// const data = fetch(url, {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json'
// },
// body: JSON.stringify(newuser)
// })
// data.then((response)=>{
// if(!response.ok){
// throw new Error('POST is not working')
// }
// return response.json()
// })
// .then((data)=>{
// console.log("============",data);
// })
// .catch((err)=>{
// console.log(err);
// })
const data=fetch(url,{
method:'DELETE',
})
data.then((response)=>{
if(!response.ok){
throw new Error('Delete not working')
}
return response.json()
})
.then((data)=>{
console.log("===========",data);
})
.catch((err)=>{
console.log("========",err);
})