-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-fetch.js
123 lines (90 loc) · 3.3 KB
/
node-fetch.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
//import fetch from 'node-fetch';
// or
const fetch = require('node-fetch');
// if you are using your own Promise library, set it through fetch.Promise. Eg.
// import Bluebird from 'bluebird';
// fetch.Promise = Bluebird;
// plain text or html
fetch('https://github.com/')
.then(res => res.text())
.then(body => console.log(body));
// json
fetch('https://api.github.com/users/github')
.then(res => res.json())
.then(json => console.log(json));
// catching network error
// 3xx-5xx responses are NOT network errors, and should be handled in then()
// you only need one catch() at the end of your promise chain
fetch('http://domain.invalid/')
.catch(err => console.error(err));
// stream
// the node.js way is to use stream when possible
fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
.then(res => {
const dest = fs.createWriteStream('./octocat.png');
res.body.pipe(dest);
});
// buffer
// if you prefer to cache binary data in full, use buffer()
// note that buffer() is a node-fetch only API
// import fileType from 'file-type';
// fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
// .then(res => res.buffer())
// .then(buffer => fileType(buffer))
// .then(type => { /* ... */ });
// meta
fetch('https://github.com/')
.then(res => {
console.log(res.ok);
console.log(res.status);
console.log(res.statusText);
console.log(res.headers.raw());
console.log(res.headers.get('content-type'));
});
// post
fetch('http://httpbin.org/post', { method: 'POST', body: 'a=1' })
.then(res => res.json())
.then(json => console.log(json));
// post with stream from file
// import { createReadStream } from 'fs';
// const stream = createReadStream('input.txt');
// fetch('http://httpbin.org/post', { method: 'POST', body: stream })
// .then(res => res.json())
// .then(json => console.log(json));
// post with JSON
var body = { a: 1 };
fetch('http://httpbin.org/post', {
method: 'POST',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' },
})
.then(res => res.json())
.then(json => console.log(json));
// post form parameters (x-www-form-urlencoded)
// import { URLSearchParams } from 'url';
// const params = new URLSearchParams();
// params.append('a', 1);
// fetch('http://httpbin.org/post', { method: 'POST', body: params })
// .then(res => res.json())
// .then(json => console.log(json));
// post with form-data (detect multipart)
// import FormData from 'form-data';
// const form = new FormData();
// form.append('a', 1);
// fetch('http://httpbin.org/post', { method: 'POST', body: form })
// .then(res => res.json())
// .then(json => console.log(json));
// post with form-data (custom headers)
// note that getHeaders() is non-standard API
// import FormData from 'form-data';
// const form = new FormData();
// form.append('a', 1);
// fetch('http://httpbin.org/post', { method: 'POST', body: form, headers: form.getHeaders() })
// .then(res => res.json())
// .then(json => console.log(json));
// node 7+ with async function
(async function () {
const res = await fetch('https://api.github.com/users/github');
const json = await res.json();
console.log(json);
})();