-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswapi.js
76 lines (69 loc) · 1.76 KB
/
swapi.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
fetch('https://swapi.co/api/people')
.then(res => res.json())
.then(peeps => console.log(peeps))
.catch(err => console.log(err))
// .then( function(res){
// return res.json()
// })
// GET A LIST OF PLANETS loop through results using es6 and display the name
fetch('https://swapi.co/api/planets')
.then(res => res.json())
.then(planets => {
planets.results.map(p => console.log(p.name))
})
.catch(err => console.log(err))
// THEN A SINGLE PLANET
fetch('https://swapi.co/api/planets/1')
.then(res => res.json())
.then(planet => console.log(planet.name))
// the rails shoes api
fetch('http://localhost:3000/shoes.json')
.then(res => res.json())
.then(shoes => console.log(shoes))
// get a single shoe
fetch('http://localhost:3000/shoes/1.json')
.then(res => res.json())
.then(shoe => console.log(shoe))
// create a new shoe using fetch
let vans = {
shoe: {
brand: 'vans',
color: 'red',
imgURL: 'https://images-na.ssl-images-amazon.com/images/I/81lWpSPpfGL._UX395_.jpg',
size: 11
}
}
fetch('http://localhost:3000/shoes', {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(vans)
})
.then(res => res.json())
.then(shoe => console.log(shoe))
// update an existing shoe using fetch
let vans = {
shoe: {
color: 'green'
}
}
fetch('http://localhost:3000/shoes/2', {
method: 'PUT',
headers: {
'content-type': 'application/json',
'accept': 'application/json'
},
body: JSON.stringify(vans)
})
.then(res => res.json())
.then(shoe => console.log(shoe))
//delet a shoe from the database
fetch('http://localhost:3000/shoes/2', {
method: 'DELETE',
headers: {
'content-type': 'application/json',
'accept': 'application/json'
}
})
.then(res => console.log(res))