|
<h1>Form-Data <a href="https://www.npmjs.com/package/form-data" rel="nofollow"><img src="https://img.shields.io/npm/v/form-data.svg" alt="NPM Module"></a> <a href="https://gitter.im/form-data/form-data" rel="nofollow"><img src="http://form-data.github.io/images/gitterbadge.svg"></a></h1>
<p>A library to create readable <code>"multipart/form-data"</code> streams. Can be used to submit forms and file uploads to other web applications.</p>
<p>The API of this library is inspired by the <a href="http://dev.w3.org/2006/webapi/XMLHttpRequest-2/Overview.html#the-formdata-interface" rel="nofollow">XMLHttpRequest-2 FormData Interface</a>.</p>
<p><a href="https://travis-ci.org/form-data/form-data" rel="nofollow"><img src="https://img.shields.io/travis/form-data/form-data/master.svg?label=linux:4.x-9.x" alt="Linux Build"></a>
<a href="https://travis-ci.org/form-data/form-data" rel="nofollow"><img src="https://img.shields.io/travis/form-data/form-data/master.svg?label=macos:4.x-9.x" alt="MacOS Build"></a>
<a href="https://ci.appveyor.com/project/alexindigo/form-data" rel="nofollow"><img src="https://img.shields.io/appveyor/ci/alexindigo/form-data/master.svg?label=windows:4.x-9.x" alt="Windows Build"></a></p>
<p><a href="https://coveralls.io/github/form-data/form-data?branch=master" rel="nofollow"><img src="https://img.shields.io/coveralls/form-data/form-data/master.svg?label=code+coverage" alt="Coverage Status"></a>
<a href="https://david-dm.org/form-data/form-data" rel="nofollow"><img src="https://img.shields.io/david/form-data/form-data.svg" alt="Dependency Status"></a>
<a href="https://www.bithound.io/github/form-data/form-data" rel="nofollow"><img src="https://www.bithound.io/github/form-data/form-data/badges/score.svg" alt="bitHound Overall Score"></a></p>
<h2>Install</h2>
<pre><code>npm install --save form-data
</code></pre>
<h2>Usage</h2>
<p>In this example we are constructing a form with 3 fields that contain a string,
a buffer and a file stream.</p>
<pre><code class="language-javascript">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'));
</code></pre>
<p>Also you can use http-response stream:</p>
<pre><code class="language-javascript">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);
});
</code></pre>
<p>Or @mikeal's <a href="https://github.com/request/request" rel="nofollow">request</a> stream:</p>
<pre><code class="language-javascript">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'));
</code></pre>
<p>In order to submit this form to a web application, call <code>submit(url, [callback])</code> method:</p>
<pre><code class="language-javascript">form.submit('http://example.org/', function(err, res) {
// res – response object (http.IncomingMessage) //
res.resume();
});
</code></pre>
<p>For more advanced request manipulations <code>submit()</code> method returns <code>http.ClientRequest</code> object, or you can choose from one of the alternative submission methods.</p>
<h3>Custom options</h3>
<p>You can provide custom options, such as <code>maxDataSize</code>:</p>
<pre><code class="language-javascript">var FormData = require('form-data');
var form = new FormData({ maxDataSize: 20971520 });
form.append('my_field', 'my value');
form.append('my_buffer', /* something big */);
</code></pre>
<p>List of available options could be found in <a href="https://github.com/felixge/node-combined-stream/blob/master/lib/combined_stream.js#L7-L15" rel="nofollow">combined-stream</a></p>
<h3>Alternative submission methods</h3>
<p>You can use node's http client interface:</p>
<pre><code class="language-javascript">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);
});
</code></pre>
<p>Or if you would prefer the <code>'Content-Length'</code> header to be set for you:</p>
<pre><code class="language-javascript">form.submit('example.org/upload', function(err, res) {
console.log(res.statusCode);
});
</code></pre>
<p>To use custom headers and pre-known length in parts:</p>
<pre><code class="language-javascript">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');
});
</code></pre>
<p>Form-Data can recognize and fetch all the required information from common types of streams (<code>fs.readStream</code>, <code>http.response</code> and <code>mikeal's request</code>), for some other types of streams you'd need to provide "file"-related information manually:</p>
<pre><code class="language-javascript">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');
});
});
</code></pre>
<p>The <code>filepath</code> property overrides <code>filename</code> and may contain a relative path. This is typically used when uploading <a href="https://wicg.github.io/entries-api/#dom-htmlinputelement-webkitdirectory" rel="nofollow">multiple files from a directory</a>.</p>
<p>For edge cases, like POST request to URL with query string or to pass HTTP auth credentials, object can be passed to <code>form.submit()</code> as first parameter:</p>
<pre><code class="language-javascript">form.submit({
host: 'example.com',
path: '/probably.php?extra=params',
auth: 'username:password'
}, function(err, res) {
console.log(res.statusCode);
});
</code></pre>
<p>In case you need to also send custom HTTP headers with the POST request, you can use the <code>headers</code> key in first parameter of <code>form.submit()</code>:</p>
<pre><code class="language-javascript">form.submit({
host: 'example.com',
path: '/surelynot.php',
headers: {'x-test-header': 'test-header-value'}
}, function(err, res) {
console.log(res.statusCode);
});
</code></pre>
<h3>Integration with other libraries</h3>
<h4>Request</h4>
<p>Form submission using <a href="https://github.com/request/request" rel="nofollow">request</a>:</p>
<pre><code class="language-javascript">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);
});
</code></pre>
<p>For more details see <a href="https://github.com/request/request#multipartform-data-multipart-form-uploads" rel="nofollow">request readme</a>.</p>
<h4>node-fetch</h4>
<p>You can also submit a form using <a href="https://github.com/bitinn/node-fetch" rel="nofollow">node-fetch</a>:</p>
<pre><code class="language-javascript">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);
});
</code></pre>
<h2>Notes</h2>
<ul>
<li><code>getLengthSync()</code> method DOESN'T calculate length for streams, use <code>knownLength</code> options as workaround.</li>
<li>Starting version <code>2.x</code> FormData has dropped support for <code>node@0.10.x</code>.</li>
</ul>
<h2>License</h2>
<p>Form-Data is released under the <a href="/zhengbingbing/ZLJ_UI_V6.0/src/9980a48d8008399e53be1d869e798386aed8c720/ZljCallCenterWeb.MiniProgram/node_modules/form-data/License" rel="nofollow">MIT</a> license.</p>
|