Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casts anything but buffers and streams to strings on appends. #362

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 5 additions & 13 deletions lib/form_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var path = require('path');
var http = require('http');
var https = require('https');
var parseUrl = require('url').parse;
var Readable = require('stream').Readable;
var fs = require('fs');
var mime = require('mime-types');
var asynckit = require('asynckit');
Expand Down Expand Up @@ -46,21 +47,12 @@ FormData.prototype.append = function(field, value, options) {
options = {filename: options};
}

var append = CombinedStream.prototype.append.bind(this);

// all that streamy business can't handle numbers
if (typeof value == 'number') {
value = '' + value;
}

// https://github.com/felixge/node-form-data/issues/38
if (util.isArray(value)) {
// Please convert your array into string
// the way web server expects it
this._error(new Error('Arrays are not supported.'));
return;
// Convert everything but buffers and streams to strings.
if (!value || !(Buffer.isBuffer(value) || value instanceof Readable)) {
value = String(value);
}

var append = CombinedStream.prototype.append.bind(this);
var header = this._multiPartHeader(field, value, options);
var footer = this._multiPartFooter();

Expand Down
17 changes: 0 additions & 17 deletions test/integration/test-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,6 @@ var path = require('path');
var fs = require('fs');
var http = require('http');

// https://github.com/felixge/node-form-data/issues/38
(function testAppendArray() {

var form = new FormData();

var callback = fake.callback('testAppendArray-onError-append');
fake.expectAnytime(callback, ['Arrays are not supported.']);

form.on('error', function(err) {
// workaround for expectAnytime handling objects
callback(err.message);
});

form.append('my_array', ['bird', 'cute']);
})();


(function testGetLengthSync() {
var fields = [
{
Expand Down