This commit is contained in:
David Frank 2015-01-28 01:34:43 +08:00
parent 955b0c0719
commit bf874d06bb
1 changed files with 20 additions and 5 deletions

View File

@ -74,34 +74,49 @@ fetch('https://github.com/')
// post // post
fetch('http://httpbin.org/post', { body: 'a=1' }) fetch('http://httpbin.org/post', { method: 'POST', body: 'a=1' })
.then(function(res) { .then(function(res) {
return res.json(); return res.json();
}).then(function(json) { }).then(function(json) {
console.log(json); console.log(json);
}); });
// post with stream // post with stream from resumer
var resumer = require('resumer'); var resumer = require('resumer');
fetch('http://httpbin.org/post', { body: resumer().queue('a=1').end() }) var stream = resumer().queue('a=1').end();
fetch('http://httpbin.org/post', { method: 'POST', body: stream })
.then(function(res) { .then(function(res) {
return res.json(); return res.json();
}).then(function(json) { }).then(function(json) {
console.log(json); console.log(json);
}); });
// post with formdata // post with form-data
var FormData = require('form-data'); var FormData = require('form-data');
var form = new FormData(); var form = new FormData();
form.append('a', 1); form.append('a', 1);
fetch('http://httpbin.org/post', { body: form, headers: form.getHeaders() }) fetch('http://httpbin.org/post', { method: 'POST', body: form, headers: form.getHeaders() })
.then(function(res) { .then(function(res) {
return res.json(); return res.json();
}).then(function(json) { }).then(function(json) {
console.log(json); console.log(json);
}); });
// node 0.11+, yield with co
var co = require('co');
co(function *() {
var res = yield fetch('https://api.github.com/users/github')
.then(function(res) {
return res.json();
}).then(function(json) {
console.log(json);
});
console.log(res);
});
``` ```
See [test cases](https://github.com/bitinn/node-fetch/blob/master/test/test.js) for more examples. See [test cases](https://github.com/bitinn/node-fetch/blob/master/test/test.js) for more examples.