call tostring on plain object body

This commit is contained in:
David Frank 2016-09-11 22:33:22 +08:00
parent e4a7da8326
commit f17aa9af1f
2 changed files with 19 additions and 0 deletions

View File

@ -229,6 +229,7 @@ function Fetch(url, opts) {
});
// accept string, buffer or readable stream as body
// per spec we will call tostring on non-stream objects
if (typeof options.body === 'string') {
req.write(options.body);
req.end();
@ -237,6 +238,9 @@ function Fetch(url, opts) {
req.end()
} else if (typeof options.body === 'object' && options.body.pipe) {
options.body.pipe(req);
} else if (typeof options.body === 'object') {
req.write(options.body.toString());
req.end();
} else {
req.end();
}

View File

@ -727,6 +727,21 @@ describe('node-fetch', function() {
});
});
it('should allow POST request with object body', function() {
url = base + '/inspect';
// note that fetch simply calls tostring on an object
opts = {
method: 'POST'
, body: { a:1 }
};
return fetch(url, opts).then(function(res) {
return res.json();
}).then(function(res) {
expect(res.method).to.equal('POST');
expect(res.body).to.equal('[object Object]');
});
});
it('should allow PUT request', function() {
url = base + '/inspect';
opts = {