Skip to content

Commit

Permalink
allow non-plain object as request body (#217)
Browse files Browse the repository at this point in the history
* allow non-plain object as request body

* add tests for object request body

* fix test for non-plain object request body

* fix tests to pass linter
  • Loading branch information
bisubus authored and floatdrop committed Sep 14, 2016
1 parent be9fe6a commit 21b8b4a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const isRedirect = require('is-redirect');
const unzipResponse = require('unzip-response');
const createErrorClass = require('create-error-class');
const nodeStatusCodes = require('node-status-codes');
const isPlainObj = require('is-plain-obj');
const isRetryAllowed = require('is-retry-allowed');
const pkg = require('./package.json');

Expand Down Expand Up @@ -236,7 +235,7 @@ function normalizeArguments(url, opts) {
let body = opts.body;

if (body) {
if (typeof body !== 'string' && !Buffer.isBuffer(body) && !isStream(body) && !isPlainObj(body)) {
if (typeof body !== 'string' && !(body !== null && typeof body === 'object')) {
throw new Error('options.body must be a ReadableStream, string, Buffer or plain Object');
}

Expand All @@ -245,7 +244,7 @@ function normalizeArguments(url, opts) {
if (isStream(body) && typeof body.getBoundary === 'function') {
// Special case for https://github.com/form-data/form-data
opts.headers['content-type'] = opts.headers['content-type'] || `multipart/form-data; boundary=${body.getBoundary()}`;
} else if (isPlainObj(body)) {
} else if (body !== null && typeof body === 'object' && !Buffer.isBuffer(body) && !isStream(body)) {
opts.headers['content-type'] = opts.headers['content-type'] || 'application/x-www-form-urlencoded';
body = opts.body = querystring.stringify(body);
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"create-error-class": "^3.0.0",
"duplexer3": "^0.1.4",
"get-stream": "^2.3.0",
"is-plain-obj": "^1.0.0",
"is-redirect": "^1.0.0",
"is-retry-allowed": "^1.0.0",
"is-stream": "^1.0.0",
Expand Down
31 changes: 27 additions & 4 deletions test/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ test('GET can have body', async t => {
t.is(headers.method, 'GET');
});

test('sends null-prototype objects', async t => {
const {body} = await got(s.url, {body: Object.create(null)});
t.is(body, '');
});

test('sends plain objects', async t => {
const {body} = await got(s.url, {body: {}});
t.is(body, '');
});

test('sends non-plain objects', async t => {
class Obj {}

const {body} = await got(s.url, {body: new Obj()});
t.is(body, '');
});

test('sends strings', async t => {
const {body} = await got(s.url, {body: 'wow'});
t.is(body, 'wow');
Expand Down Expand Up @@ -97,11 +114,17 @@ test('content-length header disabled for chunked transfer-encoding', async t =>
});

test('object in options.body treated as querystring', async t => {
const {body} = await got(s.url, {
body: {
such: 'wow'
class Obj {
constructor() {
this.such = 'wow';
}
});

get ouch() {
return 'yay';
}
}

const {body} = await got(s.url, {body: new Obj()});
t.is(body, 'such=wow');
});

Expand Down

0 comments on commit 21b8b4a

Please sign in to comment.