Giter VIP home page Giter VIP logo

form-data's Introduction

Form-Data NPM Module Join the chat at https://gitter.im/form-data/form-data

A library to create readable "multipart/form-data" streams. Can be used to submit forms and file uploads to other web applications.

The API of this library is inspired by the XMLHttpRequest-2 FormData Interface.

Linux Build MacOS Build Windows Build

Coverage Status Dependency Status

Install

npm install --save form-data

Usage

In this example we are constructing a form with 3 fields that contain a string, a buffer and a file stream.

var FormData = require('form-data');
var fs = require('fs');

var form = new FormData();
form.append('my_field', 'my value');
form.append('my_buffer', new Buffer(10));
form.append('my_file', fs.createReadStream('/foo/bar.jpg'));

Also you can use http-response stream:

var FormData = require('form-data');
var http = require('http');

var form = new FormData();

http.request('http://nodejs.org/images/logo.png', function(response) {
  form.append('my_field', 'my value');
  form.append('my_buffer', new Buffer(10));
  form.append('my_logo', response);
});

Or @mikeal's request stream:

var FormData = require('form-data');
var request = require('request');

var form = new FormData();

form.append('my_field', 'my value');
form.append('my_buffer', new Buffer(10));
form.append('my_logo', request('http://nodejs.org/images/logo.png'));

In order to submit this form to a web application, call submit(url, [callback]) method:

form.submit('http://example.org/', function(err, res) {
  // res – response object (http.IncomingMessage)  //
  res.resume();
});

For more advanced request manipulations submit() method returns http.ClientRequest object, or you can choose from one of the alternative submission methods.

Custom options

You can provide custom options, such as maxDataSize:

var FormData = require('form-data');

var form = new FormData({ maxDataSize: 20971520 });
form.append('my_field', 'my value');
form.append('my_buffer', /* something big */);

List of available options could be found in combined-stream

Alternative submission methods

You can use node's http client interface:

var http = require('http');

var request = http.request({
  method: 'post',
  host: 'example.org',
  path: '/upload',
  headers: form.getHeaders()
});

form.pipe(request);

request.on('response', function(res) {
  console.log(res.statusCode);
});

Or if you would prefer the 'Content-Length' header to be set for you:

form.submit('example.org/upload', function(err, res) {
  console.log(res.statusCode);
});

To use custom headers and pre-known length in parts:

var CRLF = '\r\n';
var form = new FormData();

var options = {
  header: CRLF + '--' + form.getBoundary() + CRLF + 'X-Custom-Header: 123' + CRLF + CRLF,
  knownLength: 1
};

form.append('my_buffer', buffer, options);

form.submit('http://example.com/', function(err, res) {
  if (err) throw err;
  console.log('Done');
});

Form-Data can recognize and fetch all the required information from common types of streams (fs.readStream, http.response and mikeal's request), for some other types of streams you'd need to provide "file"-related information manually:

someModule.stream(function(err, stdout, stderr) {
  if (err) throw err;

  var form = new FormData();

  form.append('file', stdout, {
    filename: 'unicycle.jpg', // ... or:
    filepath: 'photos/toys/unicycle.jpg',
    contentType: 'image/jpeg',
    knownLength: 19806
  });

  form.submit('http://example.com/', function(err, res) {
    if (err) throw err;
    console.log('Done');
  });
});

The filepath property overrides filename and may contain a relative path. This is typically used when uploading multiple files from a directory.

For edge cases, like POST request to URL with query string or to pass HTTP auth credentials, object can be passed to form.submit() as first parameter:

form.submit({
  host: 'example.com',
  path: '/probably.php?extra=params',
  auth: 'username:password'
}, function(err, res) {
  console.log(res.statusCode);
});

In case you need to also send custom HTTP headers with the POST request, you can use the headers key in first parameter of form.submit():

form.submit({
  host: 'example.com',
  path: '/surelynot.php',
  headers: {'x-test-header': 'test-header-value'}
}, function(err, res) {
  console.log(res.statusCode);
});

Methods

Void append( String field, Mixed value [, Mixed options] )

Append data to the form. You can submit about any format (string, integer, boolean, buffer, etc.). However, Arrays are not supported and need to be turned into strings by the user.

var form = new FormData();
form.append( 'my_string', 'my value' );
form.append( 'my_integer', 1 );
form.append( 'my_boolean', true );
form.append( 'my_buffer', new Buffer(10) );
form.append( 'my_array_as_json', JSON.stringify( ['bird','cute'] ) )

You may provide a string for options, or an object.

// Set filename by providing a string for options
form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), 'bar.jpg' );

// provide an object.
form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), {filename: 'bar.jpg', contentType: 'image/jpeg', knownLength: 19806} );

Headers getHeaders( [Headers userHeaders] )

This method adds the correct content-type header to the provided array of userHeaders.

String getBoundary()

Return the boundary of the formData. By default, the boundary consists of 26 - followed by 24 numbers for example:

--------------------------515890814546601021194782

Void setBoundary(String boundary)

Set the boundary string, overriding the default behavior described above.

Note: The boundary must be unique and may not appear in the data.

Buffer getBuffer()

Return the full formdata request package, as a Buffer. You can insert this Buffer in e.g. Axios to send multipart data.

var form = new FormData();
form.append( 'my_buffer', Buffer.from([0x4a,0x42,0x20,0x52,0x6f,0x63,0x6b,0x73]) );
form.append( 'my_file', fs.readFileSync('/foo/bar.jpg') );

axios.post( 'https://example.com/path/to/api',
            form.getBuffer(),
            form.getHeaders()
          )

Note: Because the output is of type Buffer, you can only append types that are accepted by Buffer: string, Buffer, ArrayBuffer, Array, or Array-like Object. A ReadStream for example will result in an error.

Integer getLengthSync()

Same as getLength but synchronous.

Note: getLengthSync doesn't calculate streams length.

Integer getLength( function callback )

Returns the Content-Length async. The callback is used to handle errors and continue once the length has been calculated

this.getLength(function(err, length) {
  if (err) {
    this._error(err);
    return;
  }

  // add content length
  request.setHeader('Content-Length', length);

  ...
}.bind(this));

Boolean hasKnownLength()

Checks if the length of added values is known.

Request submit( params, function callback )

Submit the form to a web application.

var form = new FormData();
form.append( 'my_string', 'Hello World' );

form.submit( 'http://example.com/', function(err, res) {
  // res – response object (http.IncomingMessage)  //
  res.resume();
} );

String toString()

Returns the form data as a string. Don't use this if you are sending files or buffers, use getBuffer() instead.

Integration with other libraries

Request

Form submission using request:

var formData = {
  my_field: 'my_value',
  my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
};

request.post({url:'http://service.com/upload', formData: formData}, function(err, httpResponse, body) {
  if (err) {
    return console.error('upload failed:', err);
  }
  console.log('Upload successful!  Server responded with:', body);
});

For more details see request readme.

node-fetch

You can also submit a form using node-fetch:

var form = new FormData();

form.append('a', 1);

fetch('http://example.com', { method: 'POST', body: form })
    .then(function(res) {
        return res.json();
    }).then(function(json) {
        console.log(json);
    });

axios

In Node.js you can post a file using axios:

const form = new FormData();
const stream = fs.createReadStream(PATH_TO_FILE);

form.append('image', stream);

// In Node.js environment you need to set boundary in the header field 'Content-Type' by calling method `getHeaders`
const formHeaders = form.getHeaders();

axios.post('http://example.com', form, {
  headers: {
    ...formHeaders,
  },
})
.then(response => response)
.catch(error => error)

Notes

  • getLengthSync() method DOESN'T calculate length for streams, use knownLength options as workaround.
  • getLength(cb) will send an error as first parameter of callback if stream length cannot be calculated (e.g. send in custom streams w/o using knownLength).
  • submit will not add content-length if form length is unknown or not calculable.
  • Starting version 2.x FormData has dropped support for [email protected].
  • Starting version 3.x FormData has dropped support for [email protected].

License

Form-Data is released under the MIT license.

form-data's People

Contributors

alexindigo avatar arku avatar bodasia avatar byr-gdp avatar c-saunders avatar debajitr avatar domenic avatar dylanpiercey avatar eiriksm avatar ekkis avatar elliottsj avatar felixge avatar idralyuk avatar jbtje avatar kornelski avatar kylev avatar linusu avatar m59peacemaker avatar mapleeit avatar mildsunrise avatar niftylettuce avatar ovyerus avatar peritus avatar pgilad avatar ranman avatar scriptsmith avatar sebdeckers avatar trierra avatar vmosyaykin avatar wxt2005 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

form-data's Issues

Submit error processing

Hello
I need to have some processing of return value form.submit
For error handling is it same to have only .on('error') handler ()
or only process errors in callback function of form.submit ?
I have following code :

var requestSent = form.submit(targetUrl,function(err) {
if (err) {
deferred.reject(e);
}
console.log('Done');
});

   .......

    requestSent.on('error', function(e) {
        console.log('problem with request: ' + e.message);
        deferred.reject(e);
    });

Thanks in advance

Memory leak when uploading large files using readable stream

There is a big memory leak when doing a file upload. When appending a readable stream, it buffers entire content of the readable stream no matter if knownSize is given. I have profiled the app and the leak is from Buffer objects (more than 20, 000 of them, each have a size of 64k!). The file size more than 600M and it takes more than 600M memory, which is a big problem.
Here is the code when I test:

var FormData = require('form-data');
var http = require('http');
var fs = require('fs');


var form = new FormData();
var fPath = 'K:\\ubuntu.iso';
var stat = fs.statSync(fPath);

form.append('file', fs.createReadStream(fPath), {knownSize: stat.size});
form.submit('http://127.0.0.1:3000/upload', function(err, res) {
  if (err) throw err;
  console.log('Done');
});

I think the leak may caused by the use of deferred-stream. However I can't manage to fix it. Any ideas?

How to read JSON from a response?

I need to get more than the statusCode from a response. The webserver I connect to returns Content-Type: application/json that I need to read. How can I read that JSON?

var FormData = require('form-data');
var form = new FormData();
form.append('my_field', 'my value');

form.submit('http://example.com/upload', function(err, res) {
  console.log(res.statusCode);
  console.log(Object.keys(res)); // doesn't show any JSON keys I expect
});

Perhaps I don't see any JSON because none is being sent. Perhaps I need to add Accept: application/json. How can I do that?

Capture and examine message body

Referring to the Node HTTP client example, instead of using 'form.pipe', is there a way to capture and examine the message body as a string before it is sent?

Something like this:

var http = require('http');
var FormData = require('form-data');
var fs = require('fs');

var form = new FormData();
form.append('my_field', 'my value');
form.append('my_buffer', new Buffer(10));
form.append('my_file', fs.createReadStream('/foo/bar.jpg'));

var request = http.request({
method: 'post',
host: 'example.org',
path: '/upload',
headers: form.getHeaders()
});

// I've just made up method 'getBody', don't know if it exists
var body = form.getBody();

// Examine request body
console.log(body);

// add body to request
request.write(body);

// close request and send to server
request.end();

request.on('response', function(res) {
console.log(res.statusCode);
});

Can't set file encoding in options

I can't set any other headers for file like file encoding. Maybe use the property options to pass some headers allows more flexibility.

License

What license is this distributed under? Please add a license file to clarify.

MIT, BSD, or Apache would be preferred, but obviously this is up to you.

Thanks!

Error while appending an image file

socket.on('publication', function (postdata) {
    var form = new FormData();
    form.append('my_field', 'my_value');
    form.append('file', {
        filename: 'photo.jpg',
        ContentType: 'image/jpg',
        knownLength: 18906
    });
});

This is the error on the console:

[TypeError: Object #<Object> has no method 'on']

The idea is to send a POST request in the form-data way to another web application including all the multipart data format (the boundary and the image file itself)
Thankyou!

How to append an object?

Hello need to append object field something like:

var form = new FormData();
form.append('name', 'Name');
form.append('details', {age: 12});

but it will throw an error like this:

      TypeError: Object #<Object> has no method 'on'

      at Function.DelayedStream.create (/home/pity/workspace/node_modules/cmp-uploaders/node_modules/combined-stream/node_modules/delayed-stream/lib/delayed_stream.js:33:10)
      at FormData.CombinedStream.append (/home/pity/workspace/node_modules/cmp-uploaders/node_modules/combined-stream/lib/combined_stream.js:43:37)
      at FormData.append (/home/pity/workspace/node_modules/cmp-uploaders/node_modules/form-data/lib/form_data.js:43:3)
      at validationFailTest (/home/pity/workspace/node_modules/cmp-uploaders/test/lab/simple_uploader.js:68:6)
      at Object._onImmediate (/home/pity/workspace/node_modules/cmp-uploaders/node_modules/lab/lib/execute.js:492:17)
      at processImmediate [as _immediateCallback] (timers.js:330:15)

General form question?

What's the difference between form-data and the post body? I'm assuming that form-data is a specific format that is arranged then put in the post body as a string, am I right? Thanks in advance, sorry for putting this here. If I am right how would one convert a form object to a string / buffer?

Error content length when readstream have start and end options

I create a readstream with start and end options,the getLength get a error size

fs.createReadStream('sample.txt', {start: 90, end: 99});

lib/form_data.js line 82

      fs.stat(value.path, function(err, stat) {
        if (err) {
          next(err);
          return;
        }

        next(null, stat.size);
      });  

Help request: Sending stream with unknown length

Hi,

Firstly, much thanks for this package. While I'm stuck at the moment I'm sure it will have saved me a massive amount of effort.

I'm having a problem at the moment. I'm trying to send a stream that represents a file. I don't know the length of this stream (it's coming from busboy, which doesn't seem to expose it). At the moment I'm using the following, but I'm getting 'socket hung up' errors.

Do you have any advice?

      var busboy = new Busboy({ headers: browserReq.headers });
            busboy.on('file', function(fieldname, fileStream, filename, encoding, contentType) {
                formData.append(fieldname, fileStream, {filename:filename, encoding:encoding, contentType:contentType})
            });

Update npm

The npm version is empty, look :

$ npm install form-data
[email protected] ./node_modules/form-data 

$ tree ./node_modules/form-data/
./node_modules/form-data/
|-- package.json
`-- Readme.md

SSL/TLS Not Supported

When attempting to submit to an https endpoint, the following error is generated:

Error: Protocol:https not supported.
    at Object.exports.request (http.js:1841:11)
    at FormData.submit (/some/random/path/node_modules/form-data/lib/form_data.js:312:20)
    at Request.<anonymous> (/some/random/path/lib/Engine.js:69:16)
    at Request.emit (events.js:95:17)
    at Request.onRequestResponse (/some/random/path/node_modules/request/request.js:1139:10)
    at ClientRequest.emit (events.js:95:17)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (http.js:1689:21)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:122:23)
    at Socket.socketOnData [as ondata] (http.js:1584:20)
    at TCP.onread (net.js:528:27)

Missing `Content-Length` in headers may cause nginx 411 Length Required Error.

I'm not sure this is a bug, but content-length should be prepared in headers so that server knows when the chunk datas end.

I fixed this in my own project about uploading a file by calling getLength() in headers, but this cause a lot time when sending a big file.

As I know, chrome added content-length before sending the whole post content.

Trying to upload non-exixting file

Hello
I'm trying to process error in the case when trying to upload non-existing file as following

var form = new FormData();
var requestSent = form.submit(targetUrl,function(err,res) {
if (err){
//error processing ......
}
}

but I got exception with following error :

stream.js:94
throw er; // Unhandled stream error in pipe.

I tried wrap the call with try - catch block ,,but the exception was not caught
What is the correct way to process such error. One option to handle is to check file existence (with fs.stat or fs.exist ) before call to submit . But is there possible to handle exception without explicit check for existing ?
Thanks in advance

Unknown stream handling (content-length unknown)

Hi everyone,

This is more a documentation/enhancement discussion than anything else:

My use case was to send a ReadableStream via multipart/form-encoded (more or less a form with just a file input field) to a remote host. The remote side was 'fxied' and I can't do anything about it.
Without the desire to create temporary files the challenge was to stream the input directly to the remote host.

This turned out to be way more complicated as the length of the input stream is unknown. Whereas with chunking it is absolutely fine to have no content-length.

Request on it's own tries very hard to come up with a content-length whereas the FormData.getLength calculates the wrong length (in fact counting the boundary string length only).

With the _lengthRetrievers there is a nice approach to define the length for streams. And throwing in error causes Request.js to skip the content-length altogether.

      self._form.getLength(function (err, length) {
        if (!err) {
          self.setHeader('content-length', length)
        }

with in turn also activates chunking which is working as expected.

But to get into this handling, the value must be specially crafted. As the value has to look like a file stream as there are two different validations: Fullfilling this

if (!value || ( !value.path && !(value.readable && value.hasOwnProperty('httpVersion')) )) {

allows access for the lazy length determination.
Whereas the logic later on tests for this: value.hasOwnProperty('fd')

Therefore with this kind of code:

    var pipedStream = getMyReadableStream();
    var metadata = getMyMetaDataForReadableStream();
    pipedStream.path = metadata.fileName; //This fools the logic and activates chunking

    var formData = {
        document: {
            value: pipedStream,
            options: {
                filename: metadata.fileName,
                contentType: metadata.contentType ? metadata.contentType : 'application/octet-stream'
            }
        }
    };
request.post({url:uploadUrl, formData: formData}, callback);

Therefore wouldn't it make sense to have an additional option which provides cleaner access to "We don't known how long it is"?

Using HTTP-DELETE Method

Im using node-form-data for some API-Calls. With HTTP-Post it works perfect, but i need "DELETE"-Method too.

I tried this:

 var form = new FormData();
 form.append('api_key',  this.api_key);
 form.append("auth_token",this.auth_id);
 form.append("ids",id); // Array

        form.submit({
            host: "api.imageshack.com",
            path: "/v2/images",
            method: "DELETE"
        }, function(err, res) {

But it seems that form-data dont phase the appends right with method=delete.

It there another way to do this?

Content-type is "text/plain" when sending Buffers

I'm not entirely sure this is the expected behaviour, but there is room for improving the following:

I noticed while trying to send a "tar" compressed file through POST that the receiving end was not getting the file correctly. I was appending the file to the form like this:

var file = new Buffer([0,2,5,1,6,8,2,5,5...]); // Imagine this is binary data.
var form = new FormData();
form.append('filename.tar', file);

The content type was set to "text/plain", something which was not right for this type of data. Wouldn't it make sense to have the content type of buffers as "application/octet-stream" instead?

The only way that I found to send binary data from a Buffer was appending like this:

form.append('filename.tar', file, {filename: 'filename.tar', contentType: 'application/octet-stream'});

form-data failes on browserify due to mime package

form-data 0.1.4 uses mime 1.2.11 that does content = fs.readFileSync(file, 'ascii') which fails on mime.js:55 when called from browserify.

FYI request (which also requires mime directly, as well as requiring form-data) solved this with moving from mime to the new mime-types fork: see request/request#455

double callback on form submit if target unreachable

Submit's callback is being called twice if the service it is trying to connect to is unreachable.

var uploadFile = function(imageFile, callback) {
  console.log('uploading: ' + JSON.stringify(imageFile));
  var form = new FormData();
  form.append('entity_id', config.entities[imageFile.type]);
  form.append('name', imageFile.conventionalName);
  form.append('file', fs.createReadStream(imageFile.fullPath));
  form.append('key', imageFile.conventionalName);
  form.submit(config.imageService, function(err, res){
    if (res) {
      ...
    } else {
        console.log(err);
        var error = new Error('no response from image service');
        console.log(error);
        console.log(imageFile.conventionalName);
        callback(error);
    }
  });
}

prints:

uploading: {"fullPath":"/Users/ctomlinson/ot/treatful/treatful/media/images/merchant/1760/20131112_1321.png","conventionalName":"images/merchant/1760/20131112_1321.png","type":"merchantImage"}
{ [Error: write EPIPE] code: 'EPIPE', errno: 'EPIPE', syscall: 'write' }
[Error: no response from image service]
images/merchant/1760/20131112_1321.png
{ [Error: write EPIPE] code: 'EPIPE', errno: 'EPIPE', syscall: 'write' }
[Error: no response from image service]
images/merchant/1760/20131112_1321.png

/..../async/lib/async.js:22
            if (called) throw new Error("Callback was already called.");

stream ended unexpectedly

I am running into a very hard to diagnose bug. When I try to use the file upload feature in this module it work for some requests and not for others. Hard to trace down. Is there something obvious? The API I am talking to is build using node, express and swagger.

Bug from REQUEST:

Error: MultipartParser.end(): stream ended unexpectedly: state = PART_DATA_START
at MultipartParser.end (/Users/steve/Dropbox/github/RandD/api.formagg.io/node_modules/express/node_modules/connect/node_modules/formidable/lib/multipart_parser.js:306:12)
at IncomingMessage.IncomingForm.parse.fields (/Users/steve/Dropbox/github/RandD/api.formagg.io/node_modules/express/node_modules/connect/node_modules/formidable/lib/incoming_form.js:102:30)
at IncomingMessage.EventEmitter.emit (events.js:85:17)
at IncomingMessage._emitEnd (http.js:366:10)
at HTTPParser.parserOnMessageComplete as onMessageComplete
at Socket.socket.ondata (http.js:1690:22)
at TCP.onread (net.js:402:27)

Code:
var FormData = require('form-data')
var http = require('http');
var fs = require('fs')

var form = new FormData();
form.append('name', data.name || '');
form.append('address', data.address || '');
form.append('city', data.city || '');
form.append('state', data.state || '');
form.append('zipcode', data.zipcode || '');
form.append('email', data.email || '');
form.append('country', data.country || '');
form.append('website', data.website || '');
form.append('type', data.type || '');
form.append('description', data.description || '');
form.append('logofile', fs.createReadStream(req.files.logofile.path));

var request = http.request({
method: 'PUT',
host: 'formaggio.dev',
port: 4001,
path: '/maker/' + id + '?api_key=guest-key',
headers: form.getHeaders()
});

form.pipe(request);
request.on('response', function(requestres) {
if (requestres.statuscode == 500) {
//res.send(requestres);
} else {
res.redirect('/maker/' + id + '/detail');
}
});

request.on('error', function(err) {
console.log('Got an eror');
})

migrate to async 0.2.x

A newer better async is available but I get unmet dependecies
npm WARN unmet dependency [...]/node_modules/request/node_modules/form-data requires async@'~0.1.9' but will load
npm WARN unmet dependency [...]/node_modules/async,
npm WARN unmet dependency which is version 0.2.6

Way to ensure form is not finished

Right now we have some staggered streams coming in. We cannot always assure they will be appended by the time the previous one ends. Some way to ensure that the form should wait and submit later would be nice.

var unlock = form.setLock();
// do something for a while
unlock();

Loosen dependencies

This package depends on [email protected]. However, the mime package frequently updates the patch number to pull in additional mime types, without changing the API. (At this time, it's at v1.2.7.)

While it makes sense to lock down a deployed application or website, perhaps even using git or npm shrinkwrap, a reusable library like form-data should be a bit more flexible.

Consider switching the dependency to ~1.2.7 so that users can add additional mime types without having to bundle and patch form-data.

Similarly, the async project is extremely stable at this point. The API has been consistent for about a year, and only minor bug fixes have been applied. The last few versions have fixed some inconsistencies between null/undefined usage, added a bunch of tests, and cleaned up the readme. Consider using ~0.1.22 to be able to work with these changes.

1.0.0

Please publish the current version as 1.0.0. Too many applications depend on it so it is not really practical to break it. Can always do a 2.0.0 if you need to change the API.

Where to end, when piping?

Hi,

i'm using this construct in a "loop". Where to close/end the request?

I can't find it:
var http = require('http');

var request = http.request({
method: 'post',
host: 'example.org',
path: '/upload',
headers: form.getHeaders()
});

form.pipe(request);

request.on('response', function(res) {
console.log(res.statusCode);
});

tiny: when using http's request to submit, update the transfer-encoding header

Something like:

options.headers["Content-type"] = options.headers["Content-type"] + "; boundary=" + form.getBoundary();

where options is what you pass as the first argument to request's constructor. Otherwise whoever is receiving the form data will not know how to parse it, and return a 400 bad request.

IMO it would be nice to add this to the readme.

Empty strings cause odd behavior

Hi,

Thanks for the awesome library, I'm using it in a project I'm working on (hopjs) to drive the unit test framework/w request and I noticed that when I try to submit test forms with empty strings there are issues - but not when I submit the same forms using browsers.

When using express:

app.post("/post",function(req,res){
  console.log(req.body);
  res.send("OK");
});

with this code

  FormData = require('form-data');
  var f = new FormData();

  f.append("Y","Y");
  f.append("X","");

  f.submit("http://localhost:3000/post",function(err,res){
    console.log(err,res);
  });

Setting 'X' to an empty string causes something to go sideways :( But if I set X to a value all seems good!

Thanks,

celer

Extract data from POST form

Hello
I'm trying to use form-data module in order to upload binary files

Here is as snippet of code :

    var buffer = new Buffer(fileSize);
    var bytesRead = fs.readSync(fd, buffer, 0, fileSize); // get buffer from bin file ealier) 

    var targetUrl = 'http://localhost:3001/fileToSave';
    var FormData = require('form-data');
    var form = new FormData();

    form.append('myfield', "jjj");
    form.append('my_file', buffer);
    form.submit(targetUrl,function(err, res) {
        // res – response object (http.IncomingMessage)  //
        res.resume(); // for node-0.10.x
    });

On server side I got request as it can one . The question is : how can I extract file on server side from the POST request ? Is there missing in the sent request some parameters ( like conten-type )

Your help will be valuable

The request on got on the server side can be seen below :
"C:\Program Files\nodejs\node.exe" app.js
Express server listening on port 3001
got file to save
{ _readableState:
{ highWaterMark: 16384,
buffer: [],
length: 0,
pipes: null,
pipesCount: 0,
flowing: false,
ended: false,
endEmitted: false,
reading: false,
calledRead: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
objectMode: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events: {},
_maxListeners: 10,
socket:
{ _connecting: false,
_handle:
{ fd: null,
writeQueueSize: 0,
owner: [Circular],
onread: [Function: onread],
reading: true },
_readableState:
{ highWaterMark: 16384,
buffer: [],
length: 0,
pipes: null,
pipesCount: 0,
flowing: false,
ended: false,
endEmitted: false,
reading: true,
calledRead: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
objectMode: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events:
{ end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
drain: [Object],
timeout: [Function],
error: [Function],
close: [Object] },
_maxListeners: 10,
_writableState:
{ highWaterMark: 16384,
objectMode: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
sync: true,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
buffer: [] },
writable: true,
allowHalfOpen: true,
onend: [Function],
destroyed: false,
errorEmitted: false,
bytesRead: 299,
_bytesDispatched: 0,
_pendingData: null,
_pendingEncoding: '',
server:
{ domain: null,
_events: [Object],
_maxListeners: 10,
_connections: 1,
connections: [Getter/Setter],
_handle: [Object],
_usingSlaves: false,
_slaves: [],
allowHalfOpen: true,
httpAllowHalfOpen: false,
timeout: 120000,
_connectionKey: '4:0.0.0.0:3001' },
_idleTimeout: 120000,
_idleNext: { _idleNext: [Circular], _idlePrev: [Circular] },
_idlePrev: { _idleNext: [Circular], _idlePrev: [Circular] },
_idleStart: 1392908833151,
parser:
{ _headers: [],
_url: '',
onHeaders: [Function: parserOnHeaders],
onHeadersComplete: [Function: parserOnHeadersComplete],
onBody: [Function: parserOnBody],
onMessageComplete: [Function: parserOnMessageComplete],
socket: [Circular],
incoming: [Circular],
maxHeaderPairs: 2000,
onIncoming: [Function] },
ondata: [Function],
_paused: false,
_httpMessage:
{ domain: null,
_events: [Object],
_maxListeners: 10,
output: [],
outputEncodings: [],
writable: true,
_last: false,
chunkedEncoding: false,
shouldKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: true,
_headerSent: false,
_header: '',
_hasBody: true,
_trailer: '',
finished: false,
_hangupClose: false,
socket: [Circular],
connection: [Circular],
_headers: [Object],
_headerNames: [Object],
req: [Circular],
locals: [Function: locals] },
_peername: { address: '127.0.0.1', family: 'IPv4', port: 23903 } },
connection:
{ _connecting: false,
_handle:
{ fd: null,
writeQueueSize: 0,
owner: [Circular],
onread: [Function: onread],
reading: true },
_readableState:
{ highWaterMark: 16384,
buffer: [],
length: 0,
pipes: null,
pipesCount: 0,
flowing: false,
ended: false,
endEmitted: false,
reading: true,
calledRead: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
objectMode: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events:
{ end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
drain: [Object],
timeout: [Function],
error: [Function],
close: [Object] },
_maxListeners: 10,
_writableState:
{ highWaterMark: 16384,
objectMode: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
sync: true,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
buffer: [] },
writable: true,
allowHalfOpen: true,
onend: [Function],
destroyed: false,
errorEmitted: false,
bytesRead: 299,
_bytesDispatched: 0,
_pendingData: null,
_pendingEncoding: '',
server:
{ domain: null,
_events: [Object],
_maxListeners: 10,
_connections: 1,
connections: [Getter/Setter],
_handle: [Object],
_usingSlaves: false,
_slaves: [],
allowHalfOpen: true,
httpAllowHalfOpen: false,
timeout: 120000,
_connectionKey: '4:0.0.0.0:3001' },
_idleTimeout: 120000,
_idleNext: { _idleNext: [Circular], _idlePrev: [Circular] },
_idlePrev: { _idleNext: [Circular], _idlePrev: [Circular] },
_idleStart: 1392908833151,
parser:
{ _headers: [],
_url: '',
onHeaders: [Function: parserOnHeaders],
onHeadersComplete: [Function: parserOnHeadersComplete],
onBody: [Function: parserOnBody],
onMessageComplete: [Function: parserOnMessageComplete],
socket: [Circular],
incoming: [Circular],
maxHeaderPairs: 2000,
onIncoming: [Function] },
ondata: [Function],
_paused: false,
_httpMessage:
{ domain: null,
_events: [Object],
_maxListeners: 10,
output: [],
outputEncodings: [],
writable: true,
_last: false,
chunkedEncoding: false,
shouldKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: true,
_headerSent: false,
_header: '',
_hasBody: true,
_trailer: '',
finished: false,
_hangupClose: false,
socket: [Circular],
connection: [Circular],
_headers: [Object],
_headerNames: [Object],
req: [Circular],
locals: [Function: locals] },
_peername: { address: '127.0.0.1', family: 'IPv4', port: 23903 } },
httpVersion: '1.1',
complete: false,
headers:
{ 'content-type': 'multipart/form-data; boundary=--------------------------090402807168278388808047',
host: 'localhost:3001',
'content-length': '184446',
connection: 'keep-alive' },
trailers: {},
_pendings: [],
_pendingIndex: 0,
url: '/fileToSave',
method: 'POST',
statusCode: null,
client:
{ _connecting: false,
_handle:
{ fd: null,
writeQueueSize: 0,
owner: [Circular],
onread: [Function: onread],
reading: true },
_readableState:
{ highWaterMark: 16384,
buffer: [],
length: 0,
pipes: null,
pipesCount: 0,
flowing: false,
ended: false,
endEmitted: false,
reading: true,
calledRead: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
objectMode: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events:
{ end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
drain: [Object],
timeout: [Function],
error: [Function],
close: [Object] },
_maxListeners: 10,
_writableState:
{ highWaterMark: 16384,
objectMode: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
sync: true,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
buffer: [] },
writable: true,
allowHalfOpen: true,
onend: [Function],
destroyed: false,
errorEmitted: false,
bytesRead: 299,
_bytesDispatched: 0,
_pendingData: null,
_pendingEncoding: '',
server:
{ domain: null,
_events: [Object],
_maxListeners: 10,
_connections: 1,
connections: [Getter/Setter],
_handle: [Object],
_usingSlaves: false,
_slaves: [],
allowHalfOpen: true,
httpAllowHalfOpen: false,
timeout: 120000,
_connectionKey: '4:0.0.0.0:3001' },
_idleTimeout: 120000,
_idleNext: { _idleNext: [Circular], _idlePrev: [Circular] },
_idlePrev: { _idleNext: [Circular], _idlePrev: [Circular] },
_idleStart: 1392908833151,
parser:
{ _headers: [],
_url: '',
onHeaders: [Function: parserOnHeaders],
onHeadersComplete: [Function: parserOnHeadersComplete],
onBody: [Function: parserOnBody],
onMessageComplete: [Function: parserOnMessageComplete],
socket: [Circular],
incoming: [Circular],
maxHeaderPairs: 2000,
onIncoming: [Function] },
ondata: [Function],
_paused: false,
_httpMessage:
{ domain: null,
_events: [Object],
_maxListeners: 10,
output: [],
outputEncodings: [],
writable: true,
_last: false,
chunkedEncoding: false,
shouldKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: true,
_headerSent: false,
_header: '',
_hasBody: true,
_trailer: '',
finished: false,
_hangupClose: false,
socket: [Circular],
connection: [Circular],
_headers: [Object],
_headerNames: [Object],
req: [Circular],
locals: [Function: locals] },
_peername: { address: '127.0.0.1', family: 'IPv4', port: 23903 } },
_consuming: false,
_dumped: false,
httpVersionMajor: 1,
httpVersionMinor: 1,
upgrade: false,
originalUrl: '/fileToSave',
_parsedUrl:
{ protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: null,
pathname: '/fileToSave',
path: '/fileToSave',
href: '/fileToSave' },
query: {},
res:
{ domain: null,
_events: { finish: [Object], close: [Function: logRequest] },
_maxListeners: 10,
output: [],
outputEncodings: [],
writable: true,
_last: false,
chunkedEncoding: false,
shouldKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: true,
_headerSent: false,
_header: '',
_hasBody: true,
_trailer: '',
finished: false,
_hangupClose: false,
socket:
{ _connecting: false,
_handle: [Object],
_readableState: [Object],
readable: true,
domain: null,
_events: [Object],
_maxListeners: 10,
_writableState: [Object],
writable: true,
allowHalfOpen: true,
onend: [Function],
destroyed: false,
errorEmitted: false,
bytesRead: 299,
_bytesDispatched: 0,
_pendingData: null,
_pendingEncoding: '',
server: [Object],
_idleTimeout: 120000,
_idleNext: [Object],
_idlePrev: [Object],
_idleStart: 1392908833151,
parser: [Object],
ondata: [Function],
_paused: false,
_httpMessage: [Circular],
_peername: [Object] },
connection:
{ _connecting: false,
_handle: [Object],
_readableState: [Object],
readable: true,
domain: null,
_events: [Object],
_maxListeners: 10,
_writableState: [Object],
writable: true,
allowHalfOpen: true,
onend: [Function],
destroyed: false,
errorEmitted: false,
bytesRead: 299,
_bytesDispatched: 0,
_pendingData: null,
_pendingEncoding: '',
server: [Object],
_idleTimeout: 120000,
_idleNext: [Object],
_idlePrev: [Object],
_idleStart: 1392908833151,
parser: [Object],
ondata: [Function],
_paused: false,
_httpMessage: [Circular],
_peername: [Object] },
_headers: { 'x-powered-by': 'Express' },
_headerNames: { 'x-powered-by': 'X-Powered-By' },
req: [Circular],
locals: [Function: locals] },
next: [Function: next],
_startTime: Thu Feb 20 2014 17:07:13 GMT+0200 (Jerusalem Standard Time),
_remoteAddress: '127.0.0.1',
body: {},
originalMethod: 'POST',
_route_index: 0,
route:
{ path: '/fileToSave',
method: 'post',
callbacks: [ [Function] ],
keys: [],
regexp: /^/fileToSave/?$/i,
params: [] },
params: [] }
POST /fileToSave 200 120016ms
got file to save
{ _readableState:
{ highWaterMark: 16384,
buffer: [],
length: 0,
pipes: null,
pipesCount: 0,
flowing: false,
ended: false,
endEmitted: false,
reading: false,
calledRead: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
objectMode: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events: {},
_maxListeners: 10,
socket:
{ _connecting: false,
_handle:
{ fd: null,
writeQueueSize: 0,
owner: [Circular],
onread: [Function: onread],
reading: true },
_readableState:
{ highWaterMark: 16384,
buffer: [],
length: 0,
pipes: null,
pipesCount: 0,
flowing: false,
ended: false,
endEmitted: false,
reading: true,
calledRead: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
objectMode: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events:
{ end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
drain: [Object],
timeout: [Function],
error: [Function],
close: [Object] },
_maxListeners: 10,
_writableState:
{ highWaterMark: 16384,
objectMode: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
sync: true,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
buffer: [] },
writable: true,
allowHalfOpen: true,
onend: [Function],
destroyed: false,
errorEmitted: false,
bytesRead: 299,
_bytesDispatched: 0,
_pendingData: null,
_pendingEncoding: '',
server:
{ domain: null,
_events: [Object],
_maxListeners: 10,
_connections: 1,
connections: [Getter/Setter],
_handle: [Object],
_usingSlaves: false,
_slaves: [],
allowHalfOpen: true,
httpAllowHalfOpen: false,
timeout: 120000,
_connectionKey: '4:0.0.0.0:3001' },
_idleTimeout: 120000,
_idleNext: { _idleNext: [Circular], _idlePrev: [Circular] },
_idlePrev: { _idleNext: [Circular], _idlePrev: [Circular] },
_idleStart: 1392909103821,
parser:
{ _headers: [],
_url: '',
onHeaders: [Function: parserOnHeaders],
onHeadersComplete: [Function: parserOnHeadersComplete],
onBody: [Function: parserOnBody],
onMessageComplete: [Function: parserOnMessageComplete],
socket: [Circular],
incoming: [Circular],
maxHeaderPairs: 2000,
onIncoming: [Function] },
ondata: [Function],
_paused: false,
_httpMessage:
{ domain: null,
_events: [Object],
_maxListeners: 10,
output: [],
outputEncodings: [],
writable: true,
_last: false,
chunkedEncoding: false,
shouldKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: true,
_headerSent: false,
_header: '',
_hasBody: true,
_trailer: '',
finished: false,
_hangupClose: false,
socket: [Circular],
connection: [Circular],
_headers: [Object],
_headerNames: [Object],
req: [Circular],
locals: [Function: locals] },
_peername: { address: '127.0.0.1', family: 'IPv4', port: 57262 } },
connection:
{ _connecting: false,
_handle:
{ fd: null,
writeQueueSize: 0,
owner: [Circular],
onread: [Function: onread],
reading: true },
_readableState:
{ highWaterMark: 16384,
buffer: [],
length: 0,
pipes: null,
pipesCount: 0,
flowing: false,
ended: false,
endEmitted: false,
reading: true,
calledRead: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
objectMode: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events:
{ end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
drain: [Object],
timeout: [Function],
error: [Function],
close: [Object] },
_maxListeners: 10,
_writableState:
{ highWaterMark: 16384,
objectMode: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
sync: true,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
buffer: [] },
writable: true,
allowHalfOpen: true,
onend: [Function],
destroyed: false,
errorEmitted: false,
bytesRead: 299,
_bytesDispatched: 0,
_pendingData: null,
_pendingEncoding: '',
server:
{ domain: null,
_events: [Object],
_maxListeners: 10,
_connections: 1,
connections: [Getter/Setter],
_handle: [Object],
_usingSlaves: false,
_slaves: [],
allowHalfOpen: true,
httpAllowHalfOpen: false,
timeout: 120000,
_connectionKey: '4:0.0.0.0:3001' },
_idleTimeout: 120000,
_idleNext: { _idleNext: [Circular], _idlePrev: [Circular] },
_idlePrev: { _idleNext: [Circular], _idlePrev: [Circular] },
_idleStart: 1392909103821,
parser:
{ _headers: [],
_url: '',
onHeaders: [Function: parserOnHeaders],
onHeadersComplete: [Function: parserOnHeadersComplete],
onBody: [Function: parserOnBody],
onMessageComplete: [Function: parserOnMessageComplete],
socket: [Circular],
incoming: [Circular],
maxHeaderPairs: 2000,
onIncoming: [Function] },
ondata: [Function],
_paused: false,
_httpMessage:
{ domain: null,
_events: [Object],
_maxListeners: 10,
output: [],
outputEncodings: [],
writable: true,
_last: false,
chunkedEncoding: false,
shouldKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: true,
_headerSent: false,
_header: '',
_hasBody: true,
_trailer: '',
finished: false,
_hangupClose: false,
socket: [Circular],
connection: [Circular],
_headers: [Object],
_headerNames: [Object],
req: [Circular],
locals: [Function: locals] },
_peername: { address: '127.0.0.1', family: 'IPv4', port: 57262 } },
httpVersion: '1.1',
complete: false,
headers:
{ 'content-type': 'multipart/form-data; boundary=--------------------------451967075754391782843892',
host: 'localhost:3001',
'content-length': '184446',
connection: 'keep-alive' },
trailers: {},
_pendings: [],
_pendingIndex: 0,
url: '/fileToSave',
method: 'POST',
statusCode: null,
client:
{ _connecting: false,
_handle:
{ fd: null,
writeQueueSize: 0,
owner: [Circular],
onread: [Function: onread],
reading: true },
_readableState:
{ highWaterMark: 16384,
buffer: [],
length: 0,
pipes: null,
pipesCount: 0,
flowing: false,
ended: false,
endEmitted: false,
reading: true,
calledRead: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
objectMode: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events:
{ end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
drain: [Object],
timeout: [Function],
error: [Function],
close: [Object] },
_maxListeners: 10,
_writableState:
{ highWaterMark: 16384,
objectMode: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
sync: true,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
buffer: [] },
writable: true,
allowHalfOpen: true,
onend: [Function],
destroyed: false,
errorEmitted: false,
bytesRead: 299,
_bytesDispatched: 0,
_pendingData: null,
_pendingEncoding: '',
server:
{ domain: null,
_events: [Object],
_maxListeners: 10,
_connections: 1,
connections: [Getter/Setter],
_handle: [Object],
_usingSlaves: false,
_slaves: [],
allowHalfOpen: true,
httpAllowHalfOpen: false,
timeout: 120000,
_connectionKey: '4:0.0.0.0:3001' },
_idleTimeout: 120000,
_idleNext: { _idleNext: [Circular], _idlePrev: [Circular] },
_idlePrev: { _idleNext: [Circular], _idlePrev: [Circular] },
_idleStart: 1392909103821,
parser:
{ _headers: [],
_url: '',
onHeaders: [Function: parserOnHeaders],
onHeadersComplete: [Function: parserOnHeadersComplete],
onBody: [Function: parserOnBody],
onMessageComplete: [Function: parserOnMessageComplete],
socket: [Circular],
incoming: [Circular],
maxHeaderPairs: 2000,
onIncoming: [Function] },
ondata: [Function],
_paused: false,
_httpMessage:
{ domain: null,
_events: [Object],
_maxListeners: 10,
output: [],
outputEncodings: [],
writable: true,
_last: false,
chunkedEncoding: false,
shouldKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: true,
_headerSent: false,
_header: '',
_hasBody: true,
_trailer: '',
finished: false,
_hangupClose: false,
socket: [Circular],
connection: [Circular],
_headers: [Object],
_headerNames: [Object],
req: [Circular],
locals: [Function: locals] },
_peername: { address: '127.0.0.1', family: 'IPv4', port: 57262 } },
_consuming: false,
_dumped: false,
httpVersionMajor: 1,
httpVersionMinor: 1,
upgrade: false,
originalUrl: '/fileToSave',
_parsedUrl:
{ protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: null,
pathname: '/fileToSave',
path: '/fileToSave',
href: '/fileToSave' },
query: {},
res:
{ domain: null,
_events: { finish: [Object], close: [Function: logRequest] },
_maxListeners: 10,
output: [],
outputEncodings: [],
writable: true,
_last: false,
chunkedEncoding: false,
shouldKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: true,
_headerSent: false,
_header: '',
_hasBody: true,
_trailer: '',
finished: false,
_hangupClose: false,
socket:
{ _connecting: false,
_handle: [Object],
_readableState: [Object],
readable: true,
domain: null,
_events: [Object],
_maxListeners: 10,
_writableState: [Object],
writable: true,
allowHalfOpen: true,
onend: [Function],
destroyed: false,
errorEmitted: false,
bytesRead: 299,
_bytesDispatched: 0,
_pendingData: null,
_pendingEncoding: '',
server: [Object],
_idleTimeout: 120000,
_idleNext: [Object],
_idlePrev: [Object],
_idleStart: 1392909103821,
parser: [Object],
ondata: [Function],
_paused: false,
_httpMessage: [Circular],
_peername: [Object] },
connection:
{ _connecting: false,
_handle: [Object],
_readableState: [Object],
readable: true,
domain: null,
_events: [Object],
_maxListeners: 10,
_writableState: [Object],
writable: true,
allowHalfOpen: true,
onend: [Function],
destroyed: false,
errorEmitted: false,
bytesRead: 299,
_bytesDispatched: 0,
_pendingData: null,
_pendingEncoding: '',
server: [Object],
_idleTimeout: 120000,
_idleNext: [Object],
_idlePrev: [Object],
_idleStart: 1392909103821,
parser: [Object],
ondata: [Function],
_paused: false,
_httpMessage: [Circular],
_peername: [Object] },
_headers: { 'x-powered-by': 'Express' },
_headerNames: { 'x-powered-by': 'X-Powered-By' },
req: [Circular],
locals: [Function: locals] },
next: [Function: next],
_startTime: Thu Feb 20 2014 17:11:43 GMT+0200 (Jerusalem Standard Time),
_remoteAddress: '127.0.0.1',
body: {},
originalMethod: 'POST',
_route_index: 0,
route:
{ path: '/fileToSave',
method: 'post',
callbacks: [ [Function] ],
keys: [],
regexp: /^/fileToSave/?$/i,
params: [] },
params: [] }

hanle Content-dispostion in request

am trying to test a service which does file upload .and below is the sample data captured by fiddler.how i can handle this in form-data
-----------------------------7de3c303c07ba
Content-Disposition: form-data; name="slow"

5000
-----------------------------7de3c303c07ba
Content-Disposition: form-data; name="file"; filename="short.csv"
Content-Type: application/vnd.ms-excel

user,password,post
[email protected],abcde,afdfd

Can't read body from response

Hi,

  form.submit({
        host: "localhost",
        port : 3000,
        path: '/' + rel_url,
        headers: headers
    }, function(err, res) {
        //res.resume();
        console.log(res.body); // returns null; but server's response returns a json. 
    });

P.S: It would be great to pass full url and headers to the submit method.

Thanks

Upload monitoring progress

Is there a way to listen the upload progress event just like AJAX does?

Example in Using_XMLHttpRequest

var req = new XMLHttpRequest();
req.addEventListener("progress", updateProgress, false);
// progress on transfers from the server to the client (downloads)
function updateProgress(evt) {
  if (evt.lengthComputable) {
    var percentComplete = evt.loaded / evt.total;
    ...
  } else {
    // Unable to compute progress information since the total size is unknown
  }
}

Package.json problem?

Hey there, sorry to bother, this is probably something wrong with my configuration. I run:

npm install -g form-data

Result:

npm http GET https://registry.npmjs.org/form-data
npm http 304 https://registry.npmjs.org/form-data
npm ERR! Failed to parse json
npm ERR! Unexpected end of input
npm ERR! File: /Users/eric/.npm/form-data/0.1.2/package/package.json
npm ERR! Failed to parse package.json data.
npm ERR! package.json must be actual JSON, not just JavaScript.
npm ERR! 
npm ERR! This is not a bug in npm.
npm ERR! Tell the package author to fix their package.json file. JSON.parse

npm ERR! System Darwin 13.1.0
npm ERR! command "node" "/opt/boxen/homebrew/bin/npm" "install" "-g" "[email protected]"
npm ERR! cwd /Users/eric/src/personalrecord_app
npm ERR! node -v v0.10.26
npm ERR! npm -v 1.4.7
npm ERR! file /Users/eric/.npm/form-data/0.1.2/package/package.json
npm ERR! code EJSONPARSE
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/eric/src/personalrecord_app/npm-debug.log
npm ERR! not ok code 0

Even if I completely remove my own package.json it gets the error. I am getting this trying to install npm install -g ionic and narrowed it down to this.

Appreciate any help, thanks!

FormData.append(x, []) freaks out

request/request#435

bradleymeck module-forge>node
> r=require('request')({method:'POST',url:'http://***.iriscouch.com/grunt-example'})
{ domain: null,
  _events: { pipe: { [Function: g] listener: [Function] } },
  _maxListeners: 10,
  readable: true,
  writable: true,
  method: 'POST',
  pool: {},
  dests: [],
  __isRequestRequest: true,
  uri: 
   { protocol: 'http:',
     slashes: true,
     host: '***.iriscouch.com',
     hostname: '***.iriscouch.com',
     href: 'http://***.iriscouch.com/grunt-example',
     pathname: '/grunt-example',
     path: '/grunt-example',
     port: 80 },
  _redirectsFollowed: 0,
  maxRedirects: 10,
  followRedirect: true,
  followAllRedirects: false,
  redirects: [],
  headers: { host: '***.iriscouch.com' },
  setHost: true,
  originalCookieHeader: undefined,
  _jar: undefined,
  port: 80,
  host: '***.iriscouch.com',
  clientErrorHandler: [Function],
  _parserErrorHandler: [Function],
  path: '/grunt-example',
  httpModule: 
   { parsers: 
      { name: 'parsers',
        constructor: [Function],
        max: 1000,
        list: [] },
     STATUS_CODES: 
      { '100': 'Continue',
        '101': 'Switching Protocols',
        '102': 'Processing',
        '200': 'OK',
        '201': 'Created',
        '202': 'Accepted',
        '203': 'Non-Authoritative Information',
        '204': 'No Content',
        '205': 'Reset Content',
        '206': 'Partial Content',
        '207': 'Multi-Status',
        '300': 'Multiple Choices',
        '301': 'Moved Permanently',
        '302': 'Moved Temporarily',
        '303': 'See Other',
        '304': 'Not Modified',
        '305': 'Use Proxy',
        '307': 'Temporary Redirect',
        '400': 'Bad Request',
        '401': 'Unauthorized',
        '402': 'Payment Required',
        '403': 'Forbidden',
        '404': 'Not Found',
        '405': 'Method Not Allowed',
        '406': 'Not Acceptable',
        '407': 'Proxy Authentication Required',
        '408': 'Request Time-out',
        '409': 'Conflict',
        '410': 'Gone',
        '411': 'Length Required',
        '412': 'Precondition Failed',
        '413': 'Request Entity Too Large',
        '414': 'Request-URI Too Large',
        '415': 'Unsupported Media Type',
        '416': 'Requested Range Not Satisfiable',
        '417': 'Expectation Failed',
        '418': 'I\'m a teapot',
        '422': 'Unprocessable Entity',
        '423': 'Locked',
        '424': 'Failed Dependency',
        '425': 'Unordered Collection',
        '426': 'Upgrade Required',
        '428': 'Precondition Required',
        '429': 'Too Many Requests',
        '431': 'Request Header Fields Too Large',
        '500': 'Internal Server Error',
        '501': 'Not Implemented',
        '502': 'Bad Gateway',
        '503': 'Service Unavailable',
        '504': 'Gateway Time-out',
        '505': 'HTTP Version not supported',
        '506': 'Variant Also Negotiates',
        '507': 'Insufficient Storage',
        '509': 'Bandwidth Limit Exceeded',
        '510': 'Not Extended',
        '511': 'Network Authentication Required' },
     IncomingMessage: { [Function: IncomingMessage] super_: [Object] },
     OutgoingMessage: { [Function: OutgoingMessage] super_: [Object] },
     ServerResponse: { [Function: ServerResponse] super_: [Object] },
     Agent: 
      { [Function: Agent]
        super_: [Function: EventEmitter],
        defaultMaxSockets: 5 },
     globalAgent: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        options: {},
        requests: {},
        sockets: {},
        maxSockets: 5,
        createConnection: [Function] },
     ClientRequest: { [Function: ClientRequest] super_: [Object] },
     request: [Function],
     get: [Function],
     Server: { [Function: Server] super_: [Object] },
     createServer: [Function],
     _connectionListener: [Function: connectionListener],
     Client: [Function: deprecated],
     createClient: [Function: deprecated] },
  agentClass: 
   { [Function: Agent]
     super_: [Function: EventEmitter],
     defaultMaxSockets: 5 },
  agent: 
   { domain: null,
     _events: { free: [Function] },
     _maxListeners: 10,
     options: {},
     requests: {},
     sockets: {},
     maxSockets: 5,
     createConnection: [Function] } }
> f = r.form();
{ _overheadLength: 0,
  _valueLength: 0,
  _lengthRetrievers: [],
  writable: false,
  readable: true,
  dataSize: 0,
  maxDataSize: 2097152,
  pauseStreams: true,
  _released: false,
  _streams: [],
  _currentStream: null }
> f.append('argv', [])
TypeError: Object  has no method 'on'
    at FormData.CombinedStream.append (/Users/bradleymeck/Documents/clean/module-forge/node_modules/request/node_modules/form-data/node_modules/combined-stream/lib/combined_stream.js:41:14)
    at FormData.append (/Users/bradleymeck/Documents/clean/module-forge/node_modules/request/node_modules/form-data/lib/form_data.js:33:3)
    at repl:1:4
    at REPLServer.self.eval (repl.js:109:21)
    at Interface.<anonymous> (repl.js:248:12)
    at Interface.EventEmitter.emit (events.js:96:17)
    at Interface._onLine (readline.js:200:10)
    at Interface._line (readline.js:518:8)
    at Interface._ttyWrite (readline.js:736:14)
    at ReadStream.onkeypress (readline.js:97:10)
> 

Error: MultipartParser.end(): stream ended unexpectedly: state = START

I am new to node.js . I have created a node.js server which accepts post requests . I am using phonegap android application to send image on local server.

error: Error: MultipartParser.end(): stream ended unexpectedly: state = START

Snippet of code is as shown below . My form.on('end' ... is not getting called and there by my image is not getting uploaded on to the server

app.post('/upload', function(req, res) {

            console.log("Upload Code Hit!")

            var form = new formidable.IncomingForm();
            form.parse(req, function(err, fields, files) {
                            console.log("Upload Code Hit2222!")
                            res.writeHead(200, {'content-type': 'text/plain'});
                            res.write('received upload:\n\n');

                            res.end(util.inspect({fields: fields, files: files}));

                            console.log(" form. parse ends ")
                    });
            console.log("33333!")
            form.on('end', function(fields, files) {
                    console.log(" form.in(end) hit ")
                    /* Temporary location of our uploaded file */
                    var temp_path = this.openedFiles[0].path;
                    /* The file name of the uploaded file */

                    var file_name=this.openedFiles[0].name;
                    /* Location where we want to copy the uploaded file */
                    var new_location = './uploadedFOlder/';

                    fs.copy(temp_path, new_location + file_name, function(err) {
                            if (err) {
                            console.error(err);
                            } else {
                            console.log("success!")
                            }
                            });
                    });

                    console.log("88888")

return;

});

i got
Upload Code Hit!
33333!
88888
Upload Code Hit2222!
form. parse ends
ie I am not getting the log

console.log(" form.in(end) hit ")

I added a code

form.on('error', function(err) {
console.log('ERROR!'+ err);
res.end();
});
and it shows

ERROR!Error: MultipartParser.end(): stream ended unexpectedly: state = START
can some one please help me what i am doing wrong ?

GraphicsMagick Steams Failing?

I am having trouble getting node-gm streams to be accepted by form-data, for example the following code:

gm('somefilepath.png')
  .resize(100, 100)
  .noProfile()
  .stream(function(err, stdout, stderr) {
    form = new FormData()
    form.append('file', stdout)
    form.submit('http://foo')
  })

fails with an error

events.js:71
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: Parse Error
    at Socket.socketOnData (http.js:1367:20)
    at TCP.onread (net.js:403:27)

Note that file streams work no problem with form-data, but i just can't get the GM stream. Thoughts?

Set Custom Headers

I would like to set the content-transfer-encoding header via form.append. Is this currently possible short of creating and managing ALL of the custom headers?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.