Sin descripción

README.md.bak 12KB

    <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>&#34;multipart/form-data&#34;</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/v4.0.0.svg?label=linux:6.x-12.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/v4.0.0.svg?label=macos:6.x-12.x" alt="MacOS 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/v4.0.0.svg?label=windows:6.x-12.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/v4.0.0.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></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(&#39;form-data&#39;); var fs = require(&#39;fs&#39;); var form = new FormData(); form.append(&#39;my_field&#39;, &#39;my value&#39;); form.append(&#39;my_buffer&#39;, new Buffer(10)); form.append(&#39;my_file&#39;, fs.createReadStream(&#39;/foo/bar.jpg&#39;)); </code></pre> <p>Also you can use http-response stream:</p> <pre><code class="language-javascript">var FormData = require(&#39;form-data&#39;); var http = require(&#39;http&#39;); var form = new FormData(); http.request(&#39;http://nodejs.org/images/logo.png&#39;, function(response) { form.append(&#39;my_field&#39;, &#39;my value&#39;); form.append(&#39;my_buffer&#39;, new Buffer(10)); form.append(&#39;my_logo&#39;, response); }); </code></pre> <p>Or @mikeal&#39;s <a href="https://github.com/request/request" rel="nofollow">request</a> stream:</p> <pre><code class="language-javascript">var FormData = require(&#39;form-data&#39;); var request = require(&#39;request&#39;); var form = new FormData(); form.append(&#39;my_field&#39;, &#39;my value&#39;); form.append(&#39;my_buffer&#39;, new Buffer(10)); form.append(&#39;my_logo&#39;, request(&#39;http://nodejs.org/images/logo.png&#39;)); </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(&#39;http://example.org/&#39;, 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(&#39;form-data&#39;); var form = new FormData({ maxDataSize: 20971520 }); form.append(&#39;my_field&#39;, &#39;my value&#39;); form.append(&#39;my_buffer&#39;, /* 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&#39;s http client interface:</p> <pre><code class="language-javascript">var http = require(&#39;http&#39;); var request = http.request({ method: &#39;post&#39;, host: &#39;example.org&#39;, path: &#39;/upload&#39;, headers: form.getHeaders() }); form.pipe(request); request.on(&#39;response&#39;, function(res) { console.log(res.statusCode); }); </code></pre> <p>Or if you would prefer the <code>&#39;Content-Length&#39;</code> header to be set for you:</p> <pre><code class="language-javascript">form.submit(&#39;example.org/upload&#39;, 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 = &#39;\r\n&#39;; var form = new FormData(); var options = { header: CRLF + &#39;--&#39; + form.getBoundary() + CRLF + &#39;X-Custom-Header: 123&#39; + CRLF + CRLF, knownLength: 1 }; form.append(&#39;my_buffer&#39;, buffer, options); form.submit(&#39;http://example.com/&#39;, function(err, res) { if (err) throw err; console.log(&#39;Done&#39;); }); </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&#39;s request</code>), for some other types of streams you&#39;d need to provide &#34;file&#34;-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(&#39;file&#39;, stdout, { filename: &#39;unicycle.jpg&#39;, // ... or: filepath: &#39;photos/toys/unicycle.jpg&#39;, contentType: &#39;image/jpeg&#39;, knownLength: 19806 }); form.submit(&#39;http://example.com/&#39;, function(err, res) { if (err) throw err; console.log(&#39;Done&#39;); }); }); </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: &#39;example.com&#39;, path: &#39;/probably.php?extra=params&#39;, auth: &#39;username:password&#39; }, 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: &#39;example.com&#39;, path: &#39;/surelynot.php&#39;, headers: {&#39;x-test-header&#39;: &#39;test-header-value&#39;} }, function(err, res) { console.log(res.statusCode); }); </code></pre> <h3>Methods</h3> <ul> <li><a href="https://github.com/form-data/form-data#void-append-string-field-mixed-value--mixed-options-" rel="nofollow"><em>Void</em> append( <strong>String</strong> <em>field</em>, <strong>Mixed</strong> <em>value</em> [, <strong>Mixed</strong> <em>options</em>] )</a>.</li> <li><a href="https://github.com/form-data/form-data#array-getheaders-array-userheaders-" rel="nofollow"><em>Headers</em> getHeaders( [<strong>Headers</strong> <em>userHeaders</em>] )</a></li> <li><a href="https://github.com/form-data/form-data#string-getboundary" rel="nofollow"><em>String</em> getBoundary()</a></li> <li><a href="https://github.com/form-data/form-data#void-setboundary" rel="nofollow"><em>Void</em> setBoundary()</a></li> <li><a href="https://github.com/form-data/form-data#buffer-getbuffer" rel="nofollow"><em>Buffer</em> getBuffer()</a></li> <li><a href="https://github.com/form-data/form-data#integer-getlengthsync" rel="nofollow"><em>Integer</em> getLengthSync()</a></li> <li><a href="https://github.com/form-data/form-data#integer-getlength-function-callback-" rel="nofollow"><em>Integer</em> getLength( <strong>function</strong> <em>callback</em> )</a></li> <li><a href="https://github.com/form-data/form-data#boolean-hasknownlength" rel="nofollow"><em>Boolean</em> hasKnownLength()</a></li> <li><a href="https://github.com/form-data/form-data#request-submit-params-function-callback-" rel="nofollow"><em>Request</em> submit( <em>params</em>, <strong>function</strong> <em>callback</em> )</a></li> <li><a href="https://github.com/form-data/form-data#string-tostring" rel="nofollow"><em>String</em> toString()</a></li> </ul> <h4><em>Void</em> append( <strong>String</strong> <em>field</em>, <strong>Mixed</strong> <em>value</em> [, <strong>Mixed</strong> <em>options</em>] )</h4> <p>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.</p> <pre><code class="language-javascript">var form = new FormData(); form.append( &#39;my_string&#39;, &#39;my value&#39; ); form.append( &#39;my_integer&#39;, 1 ); form.append( &#39;my_boolean&#39;, true ); form.append( &#39;my_buffer&#39;, new Buffer(10) ); form.append( &#39;my_array_as_json&#39;, JSON.stringify( [&#39;bird&#39;,&#39;cute&#39;] ) ) </code></pre> <p>You may provide a string for options, or an object.</p> <pre><code class="language-javascript">// Set filename by providing a string for options form.append( &#39;my_file&#39;, fs.createReadStream(&#39;/foo/bar.jpg&#39;), &#39;bar.jpg&#39; ); // provide an object. form.append( &#39;my_file&#39;, fs.createReadStream(&#39;/foo/bar.jpg&#39;), {filename: &#39;bar.jpg&#39;, contentType: &#39;image/jpeg&#39;, knownLength: 19806} ); </code></pre> <h4><em>Headers</em> getHeaders( [<strong>Headers</strong> <em>userHeaders</em>] )</h4> <p>This method adds the correct <code>content-type</code> header to the provided array of <code>userHeaders</code>.</p> <h4><em>String</em> getBoundary()</h4> <p>Return the boundary of the formData. By default, the boundary consists of 26 <code>-</code> followed by 24 numbers for example:</p> <pre><code class="language-javascript">--------------------------515890814546601021194782 </code></pre> <h4><em>Void</em> setBoundary(String <em>boundary</em>)</h4> <p>Set the boundary string, overriding the default behavior described above.</p> <p><em>Note: The boundary must be unique and may not appear in the data.</em></p> <h4><em>Buffer</em> getBuffer()</h4> <p>Return the full formdata request package, as a Buffer. You can insert this Buffer in e.g. Axios to send multipart data.</p> <pre><code class="language-javascript">var form = new FormData(); form.append( &#39;my_buffer&#39;, Buffer.from([0x4a,0x42,0x20,0x52,0x6f,0x63,0x6b,0x73]) ); form.append( &#39;my_file&#39;, fs.readFileSync(&#39;/foo/bar.jpg&#39;) ); axios.post( &#39;https://example.com/path/to/api&#39;, form.getBuffer(), form.getHeaders() ) </code></pre> <p><strong>Note:</strong> Because the output is of type Buffer, you can only append types that are accepted by Buffer: <em>string, Buffer, ArrayBuffer, Array, or Array-like Object</em>. A ReadStream for example will result in an error.</p> <h4><em>Integer</em> getLengthSync()</h4> <p>Same as <code>getLength</code> but synchronous.</p> <p>_Note: getLengthSync <strong>doesn&#39;t</strong> calculate streams length._</p> <h4><em>Integer</em> getLength( <strong>function</strong> <em>callback</em> )</h4> <p>Returns the <code>Content-Length</code> async. The callback is used to handle errors and continue once the length has been calculated</p> <pre><code class="language-javascript">this.getLength(function(err, length) { if (err) { this._error(err); return; } // add content length request.setHeader(&#39;Content-Length&#39;, length); ... }.bind(this)); </code></pre> <h4><em>Boolean</em> hasKnownLength()</h4> <p>Checks if the length of added values is known.</p> <h4><em>Request</em> submit( <em>params</em>, <strong>function</strong> <em>callback</em> )</h4> <p>Submit the form to a web application.</p> <pre><code class="language-javascript">var form = new FormData(); form.append( &#39;my_string&#39;, &#39;Hello World&#39; ); form.submit( &#39;http://example.com/&#39;, function(err, res) { // res – response object (http.IncomingMessage) // res.resume(); } ); </code></pre> <h4><em>String</em> toString()</h4> <p>Returns the form data as a string. Don&#39;t use this if you are sending files or buffers, use <code>getBuffer()</code> instead.</p> <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: &#39;my_value&#39;, my_file: fs.createReadStream(__dirname + &#39;/unicycle.jpg&#39;), }; request.post({url:&#39;http://service.com/upload&#39;, formData: formData}, function(err, httpResponse, body) { if (err) { return console.error(&#39;upload failed:&#39;, err); } console.log(&#39;Upload successful! Server responded with:&#39;, 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(&#39;a&#39;, 1); fetch(&#39;http://example.com&#39;, { method: &#39;POST&#39;, body: form }) .then(function(res) { return res.json(); }).then(function(json) { console.log(json); }); </code></pre> <h4>axios</h4> <p>In Node.js you can post a file using <a href="https://github.com/axios/axios" rel="nofollow">axios</a>:</p> <pre><code class="language-javascript">const form = new FormData(); const stream = fs.createReadStream(PATH_TO_FILE); form.append(&#39;image&#39;, stream); // In Node.js environment you need to set boundary in the header field &#39;Content-Type&#39; by calling method `getHeaders` const formHeaders = form.getHeaders(); axios.post(&#39;http://example.com&#39;, form, { headers: { ...formHeaders, }, }) .then(response =&gt; response) .catch(error =&gt; error) </code></pre> <h2>Notes</h2> <ul> <li><code>getLengthSync()</code> method DOESN&#39;T calculate length for streams, use <code>knownLength</code> options as workaround.</li> <li><code>getLength(cb)</code> will send an error as first parameter of callback if stream length cannot be calculated (e.g. send in custom streams w/o using <code>knownLength</code>).</li> <li><code>submit</code> will not add <code>content-length</code> if form length is unknown or not calculable.</li> <li>Starting version <code>2.x</code> FormData has dropped support for <code>node@0.10.x</code>.</li> <li>Starting version <code>3.x</code> FormData has dropped support for <code>node@4.x</code>.</li> </ul> <h2>License</h2> <p>Form-Data is released under the <a href="/tum/network_report_server/src/f814311a865778b02009177f98fbd61b09606d1a/app/node_modules/form-data/License" rel="nofollow">MIT</a> license.</p>