]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/request/node_modules/form-data/Readme.md
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / request / node_modules / form-data / Readme.md
1 # Form-Data [![NPM Module](https://img.shields.io/npm/v/form-data.svg)](https://www.npmjs.com/package/form-data) [![Join the chat at https://gitter.im/form-data/form-data](http://form-data.github.io/images/gitterbadge.svg)](https://gitter.im/form-data/form-data)
2
3 A library to create readable ```"multipart/form-data"``` streams. Can be used to submit forms and file uploads to other web applications.
4
5 The API of this library is inspired by the [XMLHttpRequest-2 FormData Interface][xhr2-fd].
6
7 [xhr2-fd]: http://dev.w3.org/2006/webapi/XMLHttpRequest-2/Overview.html#the-formdata-interface
8 [streams2-thing]: http://nodejs.org/api/stream.html#stream_compatibility_with_older_node_versions
9
10 [![Linux Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=linux:0.10-5.x)](https://travis-ci.org/form-data/form-data)
11 [![Windows Build](https://img.shields.io/appveyor/ci/alexindigo/form-data/master.svg?label=windows:0.10-5.x)](https://ci.appveyor.com/project/alexindigo/form-data)
12 [![Coverage Status](https://img.shields.io/coveralls/form-data/form-data/master.svg?label=code+coverage)](https://coveralls.io/github/form-data/form-data?branch=master)
13
14 [![Dependency Status](https://img.shields.io/david/form-data/form-data.svg)](https://david-dm.org/form-data/form-data)
15 [![Codacy Badge](https://img.shields.io/codacy/43ece80331c246179695e41f81eeffe2.svg)](https://www.codacy.com/app/form-data/form-data)
16 [![bitHound Overall Score](https://www.bithound.io/github/form-data/form-data/badges/score.svg)](https://www.bithound.io/github/form-data/form-data)
17
18 ## Install
19
20 ```
21 npm install form-data
22 ```
23
24 ## Usage
25
26 In this example we are constructing a form with 3 fields that contain a string,
27 a buffer and a file stream.
28
29 ``` javascript
30 var FormData = require('form-data');
31 var fs = require('fs');
32
33 var form = new FormData();
34 form.append('my_field', 'my value');
35 form.append('my_buffer', new Buffer(10));
36 form.append('my_file', fs.createReadStream('/foo/bar.jpg'));
37 ```
38
39 Also you can use http-response stream:
40
41 ``` javascript
42 var FormData = require('form-data');
43 var http = require('http');
44
45 var form = new FormData();
46
47 http.request('http://nodejs.org/images/logo.png', function(response) {
48   form.append('my_field', 'my value');
49   form.append('my_buffer', new Buffer(10));
50   form.append('my_logo', response);
51 });
52 ```
53
54 Or @mikeal's [request](https://github.com/request/request) stream:
55
56 ``` javascript
57 var FormData = require('form-data');
58 var request = require('request');
59
60 var form = new FormData();
61
62 form.append('my_field', 'my value');
63 form.append('my_buffer', new Buffer(10));
64 form.append('my_logo', request('http://nodejs.org/images/logo.png'));
65 ```
66
67 In order to submit this form to a web application, call ```submit(url, [callback])``` method:
68
69 ``` javascript
70 form.submit('http://example.org/', function(err, res) {
71   // res – response object (http.IncomingMessage)  //
72   res.resume();
73 });
74
75 ```
76
77 For more advanced request manipulations ```submit()``` method returns ```http.ClientRequest``` object, or you can choose from one of the alternative submission methods.
78
79 ### Alternative submission methods
80
81 You can use node's http client interface:
82
83 ``` javascript
84 var http = require('http');
85
86 var request = http.request({
87   method: 'post',
88   host: 'example.org',
89   path: '/upload',
90   headers: form.getHeaders()
91 });
92
93 form.pipe(request);
94
95 request.on('response', function(res) {
96   console.log(res.statusCode);
97 });
98 ```
99
100 Or if you would prefer the `'Content-Length'` header to be set for you:
101
102 ``` javascript
103 form.submit('example.org/upload', function(err, res) {
104   console.log(res.statusCode);
105 });
106 ```
107
108 To use custom headers and pre-known length in parts:
109
110 ``` javascript
111 var CRLF = '\r\n';
112 var form = new FormData();
113
114 var options = {
115   header: CRLF + '--' + form.getBoundary() + CRLF + 'X-Custom-Header: 123' + CRLF + CRLF,
116   knownLength: 1
117 };
118
119 form.append('my_buffer', buffer, options);
120
121 form.submit('http://example.com/', function(err, res) {
122   if (err) throw err;
123   console.log('Done');
124 });
125 ```
126
127 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:
128
129 ``` javascript
130 someModule.stream(function(err, stdout, stderr) {
131   if (err) throw err;
132
133   var form = new FormData();
134
135   form.append('file', stdout, {
136     filename: 'unicycle.jpg',
137     contentType: 'image/jpg',
138     knownLength: 19806
139   });
140
141   form.submit('http://example.com/', function(err, res) {
142     if (err) throw err;
143     console.log('Done');
144   });
145 });
146 ```
147
148 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:
149
150 ``` javascript
151 form.submit({
152   host: 'example.com',
153   path: '/probably.php?extra=params',
154   auth: 'username:password'
155 }, function(err, res) {
156   console.log(res.statusCode);
157 });
158 ```
159
160 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()`:
161
162 ``` javascript
163 form.submit({
164   host: 'example.com',
165   path: '/surelynot.php',
166   headers: {'x-test-header': 'test-header-value'}
167 }, function(err, res) {
168   console.log(res.statusCode);
169 });
170 ```
171
172 ### Integration with other libraries
173
174 #### Request
175
176 Form submission using  [request](https://github.com/request/request):
177
178 ```javascript
179 var formData = {
180   my_field: 'my_value',
181   my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
182 };
183
184 request.post({url:'http://service.com/upload', formData: formData}, function(err, httpResponse, body) {
185   if (err) {
186     return console.error('upload failed:', err);
187   }
188   console.log('Upload successful!  Server responded with:', body);
189 });
190 ```
191
192 For more details see [request readme](https://github.com/request/request#multipartform-data-multipart-form-uploads).
193
194 #### node-fetch
195
196 You can also submit a form using [node-fetch](https://github.com/bitinn/node-fetch):
197
198 ```javascript
199 var form = new FormData();
200
201 form.append('a', 1);
202
203 fetch('http://example.com', { method: 'POST', body: form })
204     .then(function(res) {
205         return res.json();
206     }).then(function(json) {
207         console.log(json);
208     });
209 ```
210
211 ## Notes
212
213 - ```getLengthSync()``` method DOESN'T calculate length for streams, use ```knownLength``` options as workaround.
214 - If it feels like FormData hangs after submit and you're on ```node-0.10```, please check [Compatibility with Older Node Versions][streams2-thing]
215
216 ## License
217
218 Form-Data is licensed under the MIT license.