call tostring on plain object body
This commit is contained in:
parent
e4a7da8326
commit
f17aa9af1f
4
index.js
4
index.js
|
@ -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();
|
||||
}
|
||||
|
|
15
test/test.js
15
test/test.js
|
@ -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 = {
|
||||
|
|
Loading…
Reference in New Issue