Skip to content

Commit

Permalink
Stringify plain Object in body as application/x-www-form-urlencoded
Browse files Browse the repository at this point in the history
Closes #70
  • Loading branch information
floatdrop committed Jul 27, 2015
1 parent 8045fc8 commit 7bea9c4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var PinkiePromise = require('pinkie-promise');
var unzipResponse = require('unzip-response');
var createErrorClass = require('create-error-class');
var nodeStatusCodes = require('node-status-codes');
var isPlainObj = require('is-plain-obj');

function requestAsEventEmitter(opts) {
opts = opts || {};
Expand Down Expand Up @@ -198,12 +199,17 @@ function normalizeArguments(url, opts) {
var body = opts.body;

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

opts.method = opts.method || 'POST';

if (isPlainObj(body)) {
opts.headers['content-type'] = 'application/x-www-form-urlencoded';
body = opts.body = querystring.stringify(body);
}

if (!opts.headers['content-length'] && !opts.headers['transfer-encoding'] && !isStream.readable(body)) {
var length = typeof body === 'string' ? Buffer.byteLength(body) : body.length;
opts.headers['content-length'] = length;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"dependencies": {
"create-error-class": "^2.0.0",
"duplexify": "^3.2.0",
"is-plain-obj": "^1.0.0",
"is-redirect": "^1.0.0",
"is-stream": "^1.0.0",
"lowercase-keys": "^1.0.0",
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Any of the [`http.request`](http://nodejs.org/api/http.html#http_http_request_op

###### body

Type: `string`, `Buffer`, `ReadableStream`
Type: `string`, `Buffer`, `ReadableStream`, `Object`

*This is mutually exclusive with stream mode.*

Expand All @@ -82,6 +82,8 @@ If present in `options` and `options.method` is not set, `options.method` will b

If `content-length` or `transfer-encoding` is not set in `options.headers` and `body` is a string or buffer, `content-length` will be set to the body length.

If `body` is a plain Object, it will be stringified and sent as `application/x-www-form-urlencoded`.

###### encoding

Type: `string`, `null`
Expand Down
4 changes: 2 additions & 2 deletions test/test-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ test('dns error message', function (t) {

test('options.body error message', function (t) {
t.throws(function () {
got(s.url, {body: {}});
}, /options.body must be a ReadableStream, string or Buffer/);
got(s.url, {body: function () {}});
}, /options.body must be a ReadableStream, string, Buffer or plain Object/);
t.end();
});

Expand Down
8 changes: 8 additions & 0 deletions test/test-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ test('post have content-length header to string', function (t) {
});
});

test('works with plain object in body', function (t) {
got(s.url, {body: {such: 'wow'}}, function (err, data) {
t.error(err);
t.equal(data, 'such=wow');
t.end();
});
});

test('cleanup', function (t) {
s.close();
t.end();
Expand Down

0 comments on commit 7bea9c4

Please sign in to comment.