Blame view

node_modules/superagent/docs/index.md 16.5 KB
f7563de62   Palak Handa   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
  
  # SuperAgent
  
   SuperAgent is light-weight progressive ajax API crafted for flexibility, readability, and a low learning curve after being frustrated with many of the existing request APIs. It also works with Node.js!
  
       request
         .post('/api/pet')
         .send({ name: 'Manny', species: 'cat' })
         .set('X-API-Key', 'foobar')
         .set('Accept', 'application/json')
         .end(function(err, res){
           if (err || !res.ok) {
             alert('Oh no! error');
           } else {
             alert('yay got ' + JSON.stringify(res.body));
           }
         });
  
  ## Test documentation
  
    The following [test documentation](docs/test.html) was generated with [Mocha's](http://mochajs.org/) "doc" reporter, and directly reflects the test suite. This provides an additional source of documentation.
  
  ## Request basics
  
   A request can be initiated by invoking the appropriate method on the `request` object, then calling `.end()` to send the request. For example a simple GET request:
  
       request
         .get('/search')
         .end(function(err, res){
  
         });
  
    A method string may also be passed:
  
      request('GET', '/search').end(callback);
  
  ES6 promises are supported. Instead of `.end()` you can call `.then()`:
  
      request('GET', '/search').then(success, failure);
  
   The __node__ client may also provide absolute urls:
  
       request
         .get('http://example.com/search')
         .end(function(err, res){
  
         });
  
    __DELETE__, __HEAD__, __PATCH__, __POST__, and __PUT__ requests can also be used, simply change the method name:
  
      request
        .head('/favicon.ico')
        .end(function(err, res){
  
        });
  
    __DELETE__ is a special-case, as it's a reserved word, so the method is named `.del()`:
  
      request
        .del('/user/1')
        .end(function(err, res){
  
        });
  
    The HTTP method defaults to __GET__, so if you wish, the following is valid:
  
       request('/search', function(err, res){
  
       });
  
  ## Setting header fields
  
    Setting header fields is simple, invoke `.set()` with a field name and value:
  
       request
         .get('/search')
         .set('API-Key', 'foobar')
         .set('Accept', 'application/json')
         .end(callback);
  
    You may also pass an object to set several fields in a single call:
  
       request
         .get('/search')
         .set({ 'API-Key': 'foobar', Accept: 'application/json' })
         .end(callback);
  
  ## GET requests
  
   The `.query()` method accepts objects, which when used with the __GET__ method will form a query-string. The following will produce the path `/search?query=Manny&range=1..5&order=desc`.
  
       request
         .get('/search')
         .query({ query: 'Manny' })
         .query({ range: '1..5' })
         .query({ order: 'desc' })
         .end(function(err, res){
  
         });
  
    Or as a single object:
  
      request
        .get('/search')
        .query({ query: 'Manny', range: '1..5', order: 'desc' })
        .end(function(err, res){
  
        });
  
    The `.query()` method accepts strings as well:
  
        request
          .get('/querystring')
          .query('search=Manny&range=1..5')
          .end(function(err, res){
  
          });
  
    Or joined:
  
        request
          .get('/querystring')
          .query('search=Manny')
          .query('range=1..5')
          .end(function(err, res){
  
          });
  
  ## HEAD requests
  
  You can also use the `.query()` method for HEAD requests. The following will produce the path `/users?email=joe@smith.com`.
  
        request
          .head('/users')
          .query({ email: 'joe@smith.com' })
          .end(function(err, res){
  
          });
  
  ## POST / PUT requests
  
    A typical JSON __POST__ request might look a little like the following, where we set the Content-Type header field appropriately, and "write" some data, in this case just a JSON string.
  
        request.post('/user')
          .set('Content-Type', 'application/json')
          .send('{"name":"tj","pet":"tobi"}')
          .end(callback)
  
    Since JSON is undoubtably the most common, it's the _default_! The following example is equivalent to the previous.
  
        request.post('/user')
          .send({ name: 'tj', pet: 'tobi' })
          .end(callback)
  
    Or using multiple `.send()` calls:
  
        request.post('/user')
          .send({ name: 'tj' })
          .send({ pet: 'tobi' })
          .end(callback)
  
    By default sending strings will set the Content-Type to `application/x-www-form-urlencoded`,
    multiple calls will be concatenated with `&`, here resulting in `name=tj&pet=tobi`:
  
        request.post('/user')
          .send('name=tj')
          .send('pet=tobi')
          .end(callback);
  
    SuperAgent formats are extensible, however by default "json" and "form" are supported. To send the data as `application/x-www-form-urlencoded` simply invoke `.type()` with "form", where the default is "json". This request will POST the body "name=tj&pet=tobi".
  
        request.post('/user')
          .type('form')
          .send({ name: 'tj' })
          .send({ pet: 'tobi' })
          .end(callback)
  
   Note: "form" is aliased as "form-data" and "urlencoded" for backwards compat.
  
  ## Setting the Content-Type
  
    The obvious solution is to use the `.set()` method:
  
       request.post('/user')
         .set('Content-Type', 'application/json')
  
    As a short-hand the `.type()` method is also available, accepting
    the canonicalized MIME type name complete with type/subtype, or
    simply the extension name such as "xml", "json", "png", etc:
  
       request.post('/user')
         .type('application/json')
  
       request.post('/user')
         .type('json')
  
       request.post('/user')
         .type('png')
  
  ## Serializing request body
  
  SuperAgent will automatically serialize JSON and forms. If you want to send the payload in a custom format, you can replace the built-in serialization with `.serialize()` method.
  
  ## Setting Accept
  
  In a similar fashion to the `.type()` method it is also possible to set the Accept header via the short hand method `.accept()`. Which references `request.types` as well allowing you to specify either the full canonicalized MIME type name as type/subtype, or the extension suffix form as "xml", "json", "png", etc for convenience:
  
       request.get('/user')
         .accept('application/json')
  
       request.get('/user')
         .accept('json')
  
       request.post('/user')
         .accept('png')
  
  ## Query strings
  
    `res.query(obj)` is a method which may be used to build up a query-string. For example populating `?format=json&dest=/login` on a __POST__:
  
      request
        .post('/')
        .query({ format: 'json' })
        .query({ dest: '/login' })
        .send({ post: 'data', here: 'wahoo' })
        .end(callback);
  
  ## Parsing response bodies
  
    SuperAgent will parse known response-body data for you, currently supporting `application/x-www-form-urlencoded`, `application/json`, and `multipart/form-data`.
  
    You can set a custom parser (that takes precedence over built-in parsers) with the `.buffer(true).parse(fn)` method. If response buffering is not enabled (`.buffer(false)`) then the `response` event will be emitted without waiting for the body parser to finish, so `response.body` won't be available.
  
  ### JSON / Urlencoded
  
    The property `res.body` is the parsed object, for example if a request responded with the JSON string '{"user":{"name":"tobi"}}', `res.body.user.name` would be "tobi". Likewise the x-www-form-urlencoded value of "user[name]=tobi" would yield the same result.
  
  ### Multipart
  
    The Node client supports _multipart/form-data_ via the [Formidable](https://github.com/felixge/node-formidable) module. When parsing multipart responses, the object `res.files` is also available to you. Suppose for example a request responds with the following multipart body:
  
      --whoop
      Content-Disposition: attachment; name="image"; filename="tobi.png"
      Content-Type: image/png
  
      ... data here ...
      --whoop
      Content-Disposition: form-data; name="name"
      Content-Type: text/plain
  
      Tobi
      --whoop--
  
    You would have the values `res.body.name` provided as "Tobi", and `res.files.image` as a `File` object containing the path on disk, filename, and other properties.
  
  ## Response properties
  
    Many helpful flags and properties are set on the `Response` object, ranging from the response text, parsed response body, header fields, status flags and more.
  
  ### Response text
  
    The `res.text` property contains the unparsed response body string. This
    property is always present for the client API, and only when the mime type
    matches "text/*", "*/json", or "x-www-form-urlencoded" by default for node. The
    reasoning is to conserve memory, as buffering text of large bodies such as multipart files or images is extremely inefficient.
  
    To force buffering see the "Buffering responses" section.
  
  ### Response body
  
    Much like SuperAgent can auto-serialize request data, it can also automatically parse it. When a parser is defined for the Content-Type, it is parsed, which by default includes "application/json" and "application/x-www-form-urlencoded". The parsed object is then available via `res.body`.
  
  ### Response header fields
  
    The `res.header` contains an object of parsed header fields, lowercasing field names much like node does. For example `res.header['content-length']`.
  
  ### Response Content-Type
  
    The Content-Type response header is special-cased, providing `res.type`, which is void of the charset (if any). For example the Content-Type of "text/html; charset=utf8" will provide "text/html" as `res.type`, and the `res.charset` property would then contain "utf8".
  
  ### Response status
  
    The response status flags help determine if the request was a success, among other useful information, making SuperAgent ideal for interacting with RESTful web services. These flags are currently defined as:
  
       var type = status / 100 | 0;
  
       // status / class
       res.status = status;
       res.statusType = type;
  
       // basics
       res.info = 1 == type;
       res.ok = 2 == type;
       res.clientError = 4 == type;
       res.serverError = 5 == type;
       res.error = 4 == type || 5 == type;
  
       // sugar
       res.accepted = 202 == status;
       res.noContent = 204 == status || 1223 == status;
       res.badRequest = 400 == status;
       res.unauthorized = 401 == status;
       res.notAcceptable = 406 == status;
       res.notFound = 404 == status;
       res.forbidden = 403 == status;
  
  ## Aborting requests
  
    To abort requests simply invoke the `req.abort()` method.
  
  ## Request timeouts
  
    A timeout can be applied by invoking `req.timeout(ms)`, after which an error
    will be triggered. To differentiate between other errors the `err.timeout` property
    is set to the `ms` value. __NOTE__ that this is a timeout applied to the request
    and all subsequent redirects, not per request.
  
  ## Authentication
  
    In both Node and browsers auth available via the `.auth()` method:
  
      request
        .get('http://local')
        .auth('tobi', 'learnboost')
        .end(callback);
  
  
    In the _Node_ client Basic auth can be in the URL as "user:pass":
  
      request.get('http://tobi:learnboost@local').end(callback);
  
    By default only `Basic` auth is used. In browser you can add `{type:'auto'}` to enable all methods built-in in the browser (Digest, NTLM, etc.):
  
      request.auth('digest', 'secret', {type:'auto'})
  
  ## Following redirects
  
    By default up to 5 redirects will be followed, however you may specify this with the `res.redirects(n)` method:
  
      request
        .get('/some.png')
        .redirects(2)
        .end(callback);
  
  ## Piping data
  
    The Node client allows you to pipe data to and from the request. For example piping a file's contents as the request:
  
      var request = require('superagent')
        , fs = require('fs');
  
      var stream = fs.createReadStream('path/to/my.json');
      var req = request.post('/somewhere');
      req.type('json');
      stream.pipe(req);
  
    Or piping the response to a file:
  
      var request = require('superagent')
        , fs = require('fs');
  
      var stream = fs.createWriteStream('path/to/my.json');
      var req = request.get('/some.json');
      req.pipe(stream);
  
  ## Multipart requests
  
    SuperAgent is also great for _building_ multipart requests for which it provides methods `.attach()` and `.field()`.
  
  ### Attaching files
  
    As mentioned a higher-level API is also provided, in the form of `.attach(name, [path], [filename])` and `.field(name, value)`. Attaching several files is simple, you can also provide a custom filename for the attachment, otherwise the basename of the attached file is used.
  
      request
        .post('/upload')
        .attach('avatar', 'path/to/tobi.png', 'user.png')
        .attach('image', 'path/to/loki.png')
        .attach('file', 'path/to/jane.png')
        .end(callback);
  
  ### Field values
  
    Much like form fields in HTML, you can set field values with the `.field(name, value)` method. Suppose you want to upload a few images with your name and email, your request might look something like this:
  
       request
         .post('/upload')
         .field('user[name]', 'Tobi')
         .field('user[email]', 'tobi@learnboost.com')
         .attach('image', 'path/to/tobi.png')
         .end(callback);
  
  ## Compression
  
    The node client supports compressed responses, best of all, you don't have to do anything! It just works.
  
  ## Buffering responses
  
    To force buffering of response bodies as `res.text` you may invoke `req.buffer()`. To undo the default of buffering for text responses such
    as "text/plain", "text/html" etc you may invoke `req.buffer(false)`.
  
    When buffered the `res.buffered` flag is provided, you may use this to
    handle both buffered and unbuffered responses in the same callback.
  
  ## CORS
  
    The `.withCredentials()` method enables the ability to send cookies
    from the origin, however only when "Access-Control-Allow-Origin" is
    _not_ a wildcard ("*"), and "Access-Control-Allow-Credentials" is "true".
  
      request
        .get('http://localhost:4001/')
        .withCredentials()
        .end(function(err, res){
          assert(200 == res.status);
          assert('tobi' == res.text);
          next();
        })
  
  ## Error handling
  
  Your callback function will always be passed two arguments: error and response. If no error occurred, the first argument will be null:
  
      request
       .post('/upload')
       .attach('image', 'path/to/tobi.png')
       .end(function(err, res){
  
       });
  
       An "error" event is also emitted, with you can listen for:
  
      request
        .post('/upload')
        .attach('image', 'path/to/tobi.png')
        .on('error', handle)
        .end(function(err, res){
  
        });
  
    Note that a 4xx or 5xx response with super agent **are** considered an error by default. For example if you get a 500 or 403 response, this status information will be available via `err.status`. Errors from such responses also contain an `err.response` field with all of the properties mentioned in "Response properties". The library behaves in this way to handle the common case of wanting success responses and treating HTTP error status codes as errors while still allowing for custom logic around specific error conditions.
  
    Network failures, timeouts, and other errors that produce no response will contain no `err.status` or `err.response` fields.
  
    If you wish to handle 404 or other HTTP error responses, you can query the `err.status` property.
    When an HTTP error occurs (4xx or 5xx response) the `res.error` property is an `Error` object,
    this allows you to perform checks such as:
  
      if (err && err.status === 404) {
        alert('oh no ' + res.body.message);
      }
      else if (err) {
        // all other error types we handle generically
      }
  
  ## Promise and Generator support
  
  SuperAgent's request is a "thenable" object that's compatible with JavaScript promises and `async`/`await` syntax.
  
  Libraries like [co](https://github.com/tj/co) or a web framework like [koa](https://github.com/koajs/koa) can `yield` on any SuperAgent method:
  
      var res = yield request
        .get('http://local')
        .auth('tobi', 'learnboost')
  
  Note that SuperAgent expects the global `Promise` object to be present. You'll need a polyfill to use promises in Internet Explorer or Node.js 0.10.
  
  
  ## Browser and node versions
  
  SuperAgent has two implementations: one for web browsers (using XHR) and one for Node.JS (using core http module). By default Browserify and WebPack will pick the browser version.
  
  If want to use WebPack to compile code for Node.JS, you *must* specify [node target](webpack.github.io/docs/configuration.html#target) in its configuration.
  
  ### Using browser version in electron
  
  [Electron](http://electron.atom.io/) developers report if you would prefer to use the browser version of SuperAgent instead of the Node version, you can `require('superagent/superagent')`. Your requests will now show up in the Chrome developer tools Network tab. Note this environment is not covered by automated test suite and not officially supported.