-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path14-Fetch API - JavaScript Tutorial for beginners.js
More file actions
52 lines (41 loc) · 1.2 KB
/
14-Fetch API - JavaScript Tutorial for beginners.js
File metadata and controls
52 lines (41 loc) · 1.2 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
// const url = 'https://api.thenewsapi.com/v1/news/top?api_token=mytoken'
// async function getData() {
// const response = await fetch(url)
// const data = await response.json()
// console.log(data)
// }
// getData()
//------------------------------------
// const url = 'https://api.spotify.com/v1/artists/0k17h0D3J5VfsdmQ1iZtE8'
// async function getData() {
// const request = new Request(url, {
// headers: {
// 'Authorization': 'Bearer mytoken'
// }
// })
// const response = await fetch(request)
// const data = await response.json()
// console.log(data)
// }
// getData()
//------------------------------------
const url = 'https://api.spotify.com/v1/artists/autogenerated'
const request = new Request(url, {
headers: {
'Authorization': 'Bearer 123'
}
})
async function getData() {
try {
const response = await fetch(request)
const data = await response.json()
if (response.status !== 200) {
console.log('Success', data)
} else {
console.log('Server Error', data.error.message)
}
} catch (error) {
console.log('Fetch Error', error)
}
}
getData()