-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPOSTMethod.js
More file actions
31 lines (26 loc) · 849 Bytes
/
POSTMethod.js
File metadata and controls
31 lines (26 loc) · 849 Bytes
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
console.log('Fetching');
// var url = "https://api.publicapis.org/entries";
async function apiFetching() {
var url = "http://127.0.0.1:3000/fetch_api"; // Replace with the actual IP and port
let apiKey = 'your api key'
const response = await fetch(url, {
method: 'GET', // for get the the from a api , you can use POST for post your data into a server
headers: {
accept: 'application/json',
Authorization: `Bearer ${url}` // this is how you can authenticate your api
},
});
if (!response.ok) {
throw new Error(`Error! status: ${response.status}`);
}
return await response.json();
}
async function main() {
try {
const result = await apiFetching();
console.log(result);
} catch (error) {
console.error("error");
}
}
main();