forked from node-fetch/node-fetch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
39 lines (30 loc) · 802 Bytes
/
example.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
const fetch = require('node-fetch');
// Plain text or HTML
(async () => {
const response = await fetch('https://github.com/');
const body = await response.text();
console.log(body);
})();
// JSON
(async () => {
const response = await fetch('https://github.com/');
const json = await response.json();
console.log(json);
})();
// Simple Post
(async () => {
const response = await fetch('https://httpbin.org/post', {method: 'POST', body: 'a=1'});
const json = await response.json();
console.log(json);
})();
// Post with JSON
(async () => {
const body = {a: 1};
const response = await fetch('https://httpbin.org/post', {
method: 'post',
body: JSON.stringify(body),
headers: {'Content-Type': 'application/json'}
});
const json = await response.json();
console.log(json);
})();