~ps-jenkins/ubuntu-push/ubuntu-vivid-proposed

« back to all changes in this revision

Viewing changes to docs/example-server/node_modules/supertest/node_modules/superagent/node_modules/form-data/Readme.md

  • Committer: Roberto Alsina
  • Date: 2014-09-05 14:57:17 UTC
  • mto: (91.179.25 automatic)
  • mto: This revision was merged to the branch mainline in revision 129.
  • Revision ID: roberto.alsina@canonical.com-20140905145717-0ufcsv27w25i1nnu
added example app server

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Form-Data [![Build Status](https://travis-ci.org/felixge/node-form-data.png?branch=master)](https://travis-ci.org/felixge/node-form-data) [![Dependency Status](https://gemnasium.com/felixge/node-form-data.png)](https://gemnasium.com/felixge/node-form-data)
 
2
 
 
3
A module 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 module 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
## Install
 
11
 
 
12
```
 
13
npm install form-data
 
14
```
 
15
 
 
16
## Usage
 
17
 
 
18
In this example we are constructing a form with 3 fields that contain a string,
 
19
a buffer and a file stream.
 
20
 
 
21
``` javascript
 
22
var FormData = require('form-data');
 
23
var fs = require('fs');
 
24
 
 
25
var form = new FormData();
 
26
form.append('my_field', 'my value');
 
27
form.append('my_buffer', new Buffer(10));
 
28
form.append('my_file', fs.createReadStream('/foo/bar.jpg'));
 
29
```
 
30
 
 
31
Also you can use http-response stream:
 
32
 
 
33
``` javascript
 
34
var FormData = require('form-data');
 
35
var http = require('http');
 
36
 
 
37
var form = new FormData();
 
38
 
 
39
http.request('http://nodejs.org/images/logo.png', function(response) {
 
40
  form.append('my_field', 'my value');
 
41
  form.append('my_buffer', new Buffer(10));
 
42
  form.append('my_logo', response);
 
43
});
 
44
```
 
45
 
 
46
Or @mikeal's request stream:
 
47
 
 
48
``` javascript
 
49
var FormData = require('form-data');
 
50
var request = require('request');
 
51
 
 
52
var form = new FormData();
 
53
 
 
54
form.append('my_field', 'my value');
 
55
form.append('my_buffer', new Buffer(10));
 
56
form.append('my_logo', request('http://nodejs.org/images/logo.png'));
 
57
```
 
58
 
 
59
In order to submit this form to a web application, call ```submit(url, [callback])``` method:
 
60
 
 
61
``` javascript
 
62
form.submit('http://example.org/', function(err, res) {
 
63
  // res – response object (http.IncomingMessage)  //
 
64
  res.resume(); // for node-0.10.x
 
65
});
 
66
 
 
67
```
 
68
 
 
69
For more advanced request manipulations ```submit()``` method returns ```http.ClientRequest``` object, or you can choose from one of the alternative submission methods.
 
70
 
 
71
### Alternative submission methods
 
72
 
 
73
You can use node's http client interface:
 
74
 
 
75
``` javascript
 
76
var http = require('http');
 
77
 
 
78
var request = http.request({
 
79
  method: 'post',
 
80
  host: 'example.org',
 
81
  path: '/upload',
 
82
  headers: form.getHeaders()
 
83
});
 
84
 
 
85
form.pipe(request);
 
86
 
 
87
request.on('response', function(res) {
 
88
  console.log(res.statusCode);
 
89
});
 
90
```
 
91
 
 
92
Or if you would prefer the `'Content-Length'` header to be set for you:
 
93
 
 
94
``` javascript
 
95
form.submit('example.org/upload', function(err, res) {
 
96
  console.log(res.statusCode);
 
97
});
 
98
```
 
99
 
 
100
To use custom headers and pre-known length in parts:
 
101
 
 
102
``` javascript
 
103
var CRLF = '\r\n';
 
104
var form = new FormData();
 
105
 
 
106
var options = {
 
107
  header: CRLF + '--' + form.getBoundary() + CRLF + 'X-Custom-Header: 123' + CRLF + CRLF,
 
108
  knownLength: 1
 
109
};
 
110
 
 
111
form.append('my_buffer', buffer, options);
 
112
 
 
113
form.submit('http://example.com/', function(err, res) {
 
114
  if (err) throw err;
 
115
  console.log('Done');
 
116
});
 
117
```
 
118
 
 
119
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:
 
120
 
 
121
``` javascript
 
122
someModule.stream(function(err, stdout, stderr) {
 
123
  if (err) throw err;
 
124
 
 
125
  var form = new FormData();
 
126
 
 
127
  form.append('file', stdout, {
 
128
    filename: 'unicycle.jpg',
 
129
    contentType: 'image/jpg',
 
130
    knownLength: 19806
 
131
  });
 
132
 
 
133
  form.submit('http://example.com/', function(err, res) {
 
134
    if (err) throw err;
 
135
    console.log('Done');
 
136
  });
 
137
});
 
138
```
 
139
 
 
140
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:
 
141
 
 
142
``` javascript
 
143
form.submit({
 
144
  host: 'example.com',
 
145
  path: '/probably.php?extra=params',
 
146
  auth: 'username:password'
 
147
}, function(err, res) {
 
148
  console.log(res.statusCode);
 
149
});
 
150
```
 
151
 
 
152
## Notes
 
153
 
 
154
- ```getLengthSync()``` method DOESN'T calculate length for streams, use ```knownLength``` options as workaround.
 
155
- If it feels like FormData hangs after submit and you're on ```node-0.10```, please check [Compatibility with Older Node Versions][streams2-thing]
 
156
 
 
157
## TODO
 
158
 
 
159
- Add new streams (0.10) support and try really hard not to break it for 0.8.x.
 
160
 
 
161
## License
 
162
 
 
163
Form-Data is licensed under the MIT license.