request @ 2.57.0 - README.md
1
2
# Request - Simplified HTTP client
3
4
[![npm package](https://nodei.co/npm/request.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/request/)
5
6
[![Build status](https://img.shields.io/travis/request/request.svg?style=flat-square)](https://travis-ci.org/request/request)
7
[![Coverage](https://img.shields.io/coveralls/request/request.svg?style=flat-square)](https://coveralls.io/r/request/request)
8
[![Dependency Status](https://img.shields.io/david/request/request.svg?style=flat-square)](https://david-dm.org/request/request)
9
[![Gitter](https://img.shields.io/badge/gitter-join_chat-blue.svg?style=flat-square)](https://gitter.im/request/request?utm_source=badge)
10
11
12
## Super simple to use
13
14
Request is designed to be the simplest way possible to make http calls. It supports HTTPS and follows redirects by default.
15
16
```js
17
var request = require('request');
18
request('http://www.google.com', function (error, response, body) {
19
if (!error && response.statusCode == 200) {
20
console.log(body) // Show the HTML for the Google homepage.
21
}
22
})
23
```
24
25
26
## Table of contents
27
28
- [Streaming](#streaming)
29
- [Forms](#forms)
30
- [HTTP Authentication](#http-authentication)
31
- [Custom HTTP Headers](#custom-http-headers)
32
- [OAuth Signing](#oauth-signing)
33
- [Proxies](#proxies)
34
- [Unix Domain Sockets](#unix-domain-sockets)
35
- [TLS/SSL Protocol](#tlsssl-protocol)
36
- [Support for HAR 1.2](#support-for-har-12)
37
- [**All Available Options**](#requestoptions-callback)
38
39
Request also offers [convenience methods](#convenience-methods) like
40
`request.defaults` and `request.post`, and there are
41
lots of [usage examples](#examples) and several
42
[debugging techniques](#debugging).
43
44
45
---
46
47
48
## Streaming
49
50
You can stream any response to a file stream.
51
52
```js
53
request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))
54
```
55
56
You can also stream a file to a PUT or POST request. This method will also check the file extension against a mapping of file extensions to content-types (in this case `application/json`) and use the proper `content-type` in the PUT request (if the headers don’t already provide one).
57
58
```js
59
fs.createReadStream('file.json').pipe(request.put('http://mysite.com/obj.json'))
60
```
61
62
Request can also `pipe` to itself. When doing so, `content-type` and `content-length` are preserved in the PUT headers.
63
64
```js
65
request.get('http://google.com/img.png').pipe(request.put('http://mysite.com/img.png'))
66
```
67
68
Request emits a "response" event when a response is received. The `response` argument will be an instance of [http.IncomingMessage](http://nodejs.org/api/http.html#http_http_incomingmessage).
69
70
```js
71
request
72
.get('http://google.com/img.png')
73
.on('response', function(response) {
74
console.log(response.statusCode) // 200
75
console.log(response.headers['content-type']) // 'image/png'
76
})
77
.pipe(request.put('http://mysite.com/img.png'))
78
```
79
80
To easily handle errors when streaming requests, listen to the `error` event before piping:
81
82
```js
83
request
84
.get('http://mysite.com/doodle.png')
85
.on('error', function(err) {
86
console.log(err)
87
})
88
.pipe(fs.createWriteStream('doodle.png'))
89
```
90
91
Now let’s get fancy.
92
93
```js
94
http.createServer(function (req, resp) {
95
if (req.url === '/doodle.png') {
96
if (req.method === 'PUT') {
97
req.pipe(request.put('http://mysite.com/doodle.png'))
98
} else if (req.method === 'GET' || req.method === 'HEAD') {
99
request.get('http://mysite.com/doodle.png').pipe(resp)
100
}
101
}
102
})
103
```
104
105
You can also `pipe()` from `http.ServerRequest` instances, as well as to `http.ServerResponse` instances. The HTTP method, headers, and entity-body data will be sent. Which means that, if you don't really care about security, you can do:
106
107
```js
108
http.createServer(function (req, resp) {
109
if (req.url === '/doodle.png') {
110
var x = request('http://mysite.com/doodle.png')
111
req.pipe(x)
112
x.pipe(resp)
113
}
114
})
115
```
116
117
And since `pipe()` returns the destination stream in ≥ Node 0.5.x you can do one line proxying. :)
118
119
```js
120
req.pipe(request('http://mysite.com/doodle.png')).pipe(resp)
121
```
122
123
Also, none of this new functionality conflicts with requests previous features, it just expands them.
124
125
```js
126
var r = request.defaults({'proxy':'http://localproxy.com'})
127
128
http.createServer(function (req, resp) {
129
if (req.url === '/doodle.png') {
130
r.get('http://google.com/doodle.png').pipe(resp)
131
}
132
})
133
```
134
135
You can still use intermediate proxies, the requests will still follow HTTP forwards, etc.
136
137
[back to top](#table-of-contents)
138
139
140
---
141
142
143
## Forms
144
145
`request` supports `application/x-www-form-urlencoded` and `multipart/form-data` form uploads. For `multipart/related` refer to the `multipart` API.
146
147
148
#### application/x-www-form-urlencoded (URL-Encoded Forms)
149
150
URL-encoded forms are simple.
151
152
```js
153
request.post('http://service.com/upload', {form:{key:'value'}})
154
// or
155
request.post('http://service.com/upload').form({key:'value'})
156
// or
157
request.post({url:'http://service.com/upload', form: {key:'value'}}, function(err,httpResponse,body){ /* ... */ })
158
```
159
160
161
#### multipart/form-data (Multipart Form Uploads)
162
163
For `multipart/form-data` we use the [form-data](https://github.com/felixge/node-form-data) library by [@felixge](https://github.com/felixge). For the most cases, you can pass your upload form data via the `formData` option.
164
165
166
```js
167
var formData = {
168
// Pass a simple key-value pair
169
my_field: 'my_value',
170
// Pass data via Buffers
171
my_buffer: new Buffer([1, 2, 3]),
172
// Pass data via Streams
173
my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
174
// Pass multiple values /w an Array
175
attachments: [
176
fs.createReadStream(__dirname + '/attachment1.jpg'),
177
fs.createReadStream(__dirname + '/attachment2.jpg')
178
],
179
// Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS}
180
// Use case: for some types of streams, you'll need to provide "file"-related information manually.
181
// See the `form-data` README for more information about options: https://github.com/felixge/node-form-data
182
custom_file: {
183
value: fs.createReadStream('/dev/urandom'),
184
options: {
185
filename: 'topsecret.jpg',
186
contentType: 'image/jpg'
187
}
188
}
189
};
190
request.post({url:'http://service.com/upload', formData: formData}, function optionalCallback(err, httpResponse, body) {
191
if (err) {
192
return console.error('upload failed:', err);
193
}
194
console.log('Upload successful! Server responded with:', body);
195
});
196
```
197
198
For advanced cases, you can access the form-data object itself via `r.form()`. This can be modified until the request is fired on the next cycle of the event-loop. (Note that this calling `form()` will clear the currently set form data for that request.)
199
200
```js
201
// NOTE: Advanced use-case, for normal use see 'formData' usage above
202
var r = request.post('http://service.com/upload', function optionalCallback(err, httpResponse, body) {...})
203
var form = r.form();
204
form.append('my_field', 'my_value');
205
form.append('my_buffer', new Buffer([1, 2, 3]));
206
form.append('custom_file', fs.createReadStream(__dirname + '/unicycle.jpg'), {filename: 'unicycle.jpg'});
207
```
208
See the [form-data README](https://github.com/felixge/node-form-data) for more information & examples.
209
210
211
#### multipart/related
212
213
Some variations in different HTTP implementations require a newline/CRLF before, after, or both before and after the boundary of a `multipart/related` request (using the multipart option). This has been observed in the .NET WebAPI version 4.0. You can turn on a boundary preambleCRLF or postamble by passing them as `true` to your request options.
214
215
```js
216
request({
217
method: 'PUT',
218
preambleCRLF: true,
219
postambleCRLF: true,
220
uri: 'http://service.com/upload',
221
multipart: [
222
{
223
'content-type': 'application/json'
224
body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
225
},
226
{ body: 'I am an attachment' },
227
{ body: fs.createReadStream('image.png') }
228
],
229
// alternatively pass an object containing additional options
230
multipart: {
231
chunked: false,
232
data: [
233
{
234
'content-type': 'application/json',
235
body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
236
},
237
{ body: 'I am an attachment' }
238
]
239
}
240
},
241
function (error, response, body) {
242
if (error) {
243
return console.error('upload failed:', error);
244
}
245
console.log('Upload successful! Server responded with:', body);
246
})
247
```
248
249
[back to top](#table-of-contents)
250
251
252
---
253
254
255
## HTTP Authentication
256
257
```js
258
request.get('http://some.server.com/').auth('username', 'password', false);
259
// or
260
request.get('http://some.server.com/', {
261
'auth': {
262
'user': 'username',
263
'pass': 'password',
264
'sendImmediately': false
265
}
266
});
267
// or
268
request.get('http://some.server.com/').auth(null, null, true, 'bearerToken');
269
// or
270
request.get('http://some.server.com/', {
271
'auth': {
272
'bearer': 'bearerToken'
273
}
274
});
275
```
276
277
If passed as an option, `auth` should be a hash containing values:
278
279
- `user` || `username`
280
- `pass` || `password`
281
- `sendImmediately` (optional)
282
- `bearer` (optional)
283
284
The method form takes parameters
285
`auth(username, password, sendImmediately, bearer)`.
286
287
`sendImmediately` defaults to `true`, which causes a basic or bearer
288
authentication header to be sent. If `sendImmediately` is `false`, then
289
`request` will retry with a proper authentication header after receiving a
290
`401` response from the server (which must contain a `WWW-Authenticate` header
291
indicating the required authentication method).
292
293
Note that you can also specify basic authentication using the URL itself, as
294
detailed in [RFC 1738](http://www.ietf.org/rfc/rfc1738.txt). Simply pass the
295
`user:password` before the host with an `@` sign:
296
297
```js
298
var username = 'username',
299
password = 'password',
300
url = 'http://' + username + ':' + password + '@some.server.com';
301
302
request({url: url}, function (error, response, body) {
303
// Do more stuff with 'body' here
304
});
305
```
306
307
Digest authentication is supported, but it only works with `sendImmediately`
308
set to `false`; otherwise `request` will send basic authentication on the
309
initial request, which will probably cause the request to fail.
310
311
Bearer authentication is supported, and is activated when the `bearer` value is
312
available. The value may be either a `String` or a `Function` returning a
313
`String`. Using a function to supply the bearer token is particularly useful if
314
used in conjuction with `defaults` to allow a single function to supply the
315
last known token at the time of sending a request, or to compute one on the fly.
316
317
[back to top](#table-of-contents)
318
319
320
---
321
322
323
## Custom HTTP Headers
324
325
HTTP Headers, such as `User-Agent`, can be set in the `options` object.
326
In the example below, we call the github API to find out the number
327
of stars and forks for the request repository. This requires a
328
custom `User-Agent` header as well as https.
329
330
```js
331
var request = require('request');
332
333
var options = {
334
url: 'https://api.github.com/repos/request/request',
335
headers: {
336
'User-Agent': 'request'
337
}
338
};
339
340
function callback(error, response, body) {
341
if (!error && response.statusCode == 200) {
342
var info = JSON.parse(body);
343
console.log(info.stargazers_count + " Stars");
344
console.log(info.forks_count + " Forks");
345
}
346
}
347
348
request(options, callback);
349
```
350
351
[back to top](#table-of-contents)
352
353
354
---
355
356
357
## OAuth Signing
358
359
[OAuth version 1.0](https://tools.ietf.org/html/rfc5849) is supported. The
360
default signing algorithm is
361
[HMAC-SHA1](https://tools.ietf.org/html/rfc5849#section-3.4.2):
362
363
```js
364
// OAuth1.0 - 3-legged server side flow (Twitter example)
365
// step 1
366
var qs = require('querystring')
367
, oauth =
368
{ callback: 'http://mysite.com/callback/'
369
, consumer_key: CONSUMER_KEY
370
, consumer_secret: CONSUMER_SECRET
371
}
372
, url = 'https://api.twitter.com/oauth/request_token'
373
;
374
request.post({url:url, oauth:oauth}, function (e, r, body) {
375
// Ideally, you would take the body in the response
376
// and construct a URL that a user clicks on (like a sign in button).
377
// The verifier is only available in the response after a user has
378
// verified with twitter that they are authorizing your app.
379
380
// step 2
381
var req_data = qs.parse(body)
382
var uri = 'https://api.twitter.com/oauth/authenticate'
383
+ '?' + qs.stringify({oauth_token: req_data.oauth_token})
384
// redirect the user to the authorize uri
385
386
// step 3
387
// after the user is redirected back to your server
388
var auth_data = qs.parse(body)
389
, oauth =
390
{ consumer_key: CONSUMER_KEY
391
, consumer_secret: CONSUMER_SECRET
392
, token: auth_data.oauth_token
393
, token_secret: req_data.oauth_token_secret
394
, verifier: auth_data.oauth_verifier
395
}
396
, url = 'https://api.twitter.com/oauth/access_token'
397
;
398
request.post({url:url, oauth:oauth}, function (e, r, body) {
399
// ready to make signed requests on behalf of the user
400
var perm_data = qs.parse(body)
401
, oauth =
402
{ consumer_key: CONSUMER_KEY
403
, consumer_secret: CONSUMER_SECRET
404
, token: perm_data.oauth_token
405
, token_secret: perm_data.oauth_token_secret
406
}
407
, url = 'https://api.twitter.com/1.1/users/show.json'
408
, qs =
409
{ screen_name: perm_data.screen_name
410
, user_id: perm_data.user_id
411
}
412
;
413
request.get({url:url, oauth:oauth, json:true}, function (e, r, user) {
414
console.log(user)
415
})
416
})
417
})
418
```
419
420
For [RSA-SHA1 signing](https://tools.ietf.org/html/rfc5849#section-3.4.3), make
421
the following changes to the OAuth options object:
422
* Pass `signature_method : 'RSA-SHA1'`
423
* Instead of `consumer_secret`, specify a `private_key` string in
424
[PEM format](http://how2ssl.com/articles/working_with_pem_files/)
425
426
For [PLAINTEXT signing](http://oauth.net/core/1.0/#anchor22), make
427
the following changes to the OAuth options object:
428
* Pass `signature_method : 'PLAINTEXT'`
429
430
To send OAuth parameters via query params or in a post body as described in The
431
[Consumer Request Parameters](http://oauth.net/core/1.0/#consumer_req_param)
432
section of the oauth1 spec:
433
* Pass `transport_method : 'query'` or `transport_method : 'body'` in the OAuth
434
options object.
435
* `transport_method` defaults to `'header'`
436
437
To use [Request Body Hash](https://oauth.googlecode.com/svn/spec/ext/body_hash/1.0/oauth-bodyhash.html) you can either
438
* Manually generate the body hash and pass it as a string `body_hash: '...'`
439
* Automatically generate the body hash by passing `body_hash: true`
440
441
[back to top](#table-of-contents)
442
443
444
---
445
446
447
## Proxies
448
449
If you specify a `proxy` option, then the request (and any subsequent
450
redirects) will be sent via a connection to the proxy server.
451
452
If your endpoint is an `https` url, and you are using a proxy, then
453
request will send a `CONNECT` request to the proxy server *first*, and
454
then use the supplied connection to connect to the endpoint.
455
456
That is, first it will make a request like:
457
458
```
459
HTTP/1.1 CONNECT endpoint-server.com:80
460
Host: proxy-server.com
461
User-Agent: whatever user agent you specify
462
```
463
464
and then the proxy server make a TCP connection to `endpoint-server`
465
on port `80`, and return a response that looks like:
466
467
```
468
HTTP/1.1 200 OK
469
```
470
471
At this point, the connection is left open, and the client is
472
communicating directly with the `endpoint-server.com` machine.
473
474
See [the wikipedia page on HTTP Tunneling](http://en.wikipedia.org/wiki/HTTP_tunnel)
475
for more information.
476
477
By default, when proxying `http` traffic, request will simply make a
478
standard proxied `http` request. This is done by making the `url`
479
section of the initial line of the request a fully qualified url to
480
the endpoint.
481
482
For example, it will make a single request that looks like:
483
484
```
485
HTTP/1.1 GET http://endpoint-server.com/some-url
486
Host: proxy-server.com
487
Other-Headers: all go here
488
489
request body or whatever
490
```
491
492
Because a pure "http over http" tunnel offers no additional security
493
or other features, it is generally simpler to go with a
494
straightforward HTTP proxy in this case. However, if you would like
495
to force a tunneling proxy, you may set the `tunnel` option to `true`.
496
497
You can also make a standard proxied `http` request by explicitly setting
498
`tunnel : false`, but **note that this will allow the proxy to see the traffic
499
to/from the destination server**.
500
501
If you are using a tunneling proxy, you may set the
502
`proxyHeaderWhiteList` to share certain headers with the proxy.
503
504
You can also set the `proxyHeaderExclusiveList` to share certain
505
headers only with the proxy and not with destination host.
506
507
By default, this set is:
508
509
```
510
accept
511
accept-charset
512
accept-encoding
513
accept-language
514
accept-ranges
515
cache-control
516
content-encoding
517
content-language
518
content-length
519
content-location
520
content-md5
521
content-range
522
content-type
523
connection
524
date
525
expect
526
max-forwards
527
pragma
528
proxy-authorization
529
referer
530
te
531
transfer-encoding
532
user-agent
533
via
534
```
535
536
Note that, when using a tunneling proxy, the `proxy-authorization`
537
header and any headers from custom `proxyHeaderExclusiveList` are
538
*never* sent to the endpoint server, but only to the proxy server.
539
540
541
### Controlling proxy behaviour using environment variables
542
543
The following environment variables are respected by `request`:
544
545
* `HTTP_PROXY` / `http_proxy`
546
* `HTTPS_PROXY` / `https_proxy`
547
* `NO_PROXY` / `no_proxy`
548
549
When `HTTP_PROXY` / `http_proxy` are set, they will be used to proxy non-SSL requests that do not have an explicit `proxy` configuration option present. Similarly, `HTTPS_PROXY` / `https_proxy` will be respected for SSL requests that do not have an explicit `proxy` configuration option. It is valid to define a proxy in one of the environment variables, but then override it for a specific request, using the `proxy` configuration option. Furthermore, the `proxy` configuration option can be explicitly set to false / null to opt out of proxying altogether for that request.
550
551
`request` is also aware of the `NO_PROXY`/`no_proxy` environment variables. These variables provide a granular way to opt out of proxying, on a per-host basis. It should contain a comma separated list of hosts to opt out of proxying. It is also possible to opt of proxying when a particular destination port is used. Finally, the variable may be set to `*` to opt out of the implicit proxy configuration of the other environment variables.
552
553
Here's some examples of valid `no_proxy` values:
554
555
* `google.com` - don't proxy HTTP/HTTPS requests to Google.
556
* `google.com:443` - don't proxy HTTPS requests to Google, but *do* proxy HTTP requests to Google.
557
* `google.com:443, yahoo.com:80` - don't proxy HTTPS requests to Google, and don't proxy HTTP requests to Yahoo!
558
* `*` - ignore `https_proxy`/`http_proxy` environment variables altogether.
559
560
[back to top](#table-of-contents)
561
562
563
---
564
565
566
## UNIX Domain Sockets
567
568
`request` supports making requests to [UNIX Domain Sockets](http://en.wikipedia.org/wiki/Unix_domain_socket). To make one, use the following URL scheme:
569
570
```js
571
/* Pattern */ 'http://unix:SOCKET:PATH'
572
/* Example */ request.get('http://unix:/absolute/path/to/unix.socket:/request/path')
573
```
574
575
Note: The `SOCKET` path is assumed to be absolute to the root of the host file system.
576
577
[back to top](#table-of-contents)
578
579
580
---
581
582
583
## TLS/SSL Protocol
584
585
TLS/SSL Protocol options, such as `cert`, `key` and `passphrase`, can be
586
set directly in `options` object, in the `agentOptions` property of the `options` object, or even in `https.globalAgent.options`. Keep in mind that, although `agentOptions` allows for a slightly wider range of configurations, the recommendend way is via `options` object directly, as using `agentOptions` or `https.globalAgent.options` would not be applied in the same way in proxied environments (as data travels through a TLS connection instead of an http/https agent).
587
588
```js
589
var fs = require('fs')
590
, path = require('path')
591
, certFile = path.resolve(__dirname, 'ssl/client.crt')
592
, keyFile = path.resolve(__dirname, 'ssl/client.key')
593
, caFile = path.resolve(__dirname, 'ssl/ca.cert.pem')
594
, request = require('request');
595
596
var options = {
597
url: 'https://api.some-server.com/',
598
cert: fs.readFileSync(certFile),
599
key: fs.readFileSync(keyFile),
600
passphrase: 'password',
601
ca: fs.readFileSync(caFile)
602
}
603
};
604
605
request.get(options);
606
```
607
608
### Using `options.agentOptions`
609
610
In the example below, we call an API requires client side SSL certificate
611
(in PEM format) with passphrase protected private key (in PEM format) and disable the SSLv3 protocol:
612
613
```js
614
var fs = require('fs')
615
, path = require('path')
616
, certFile = path.resolve(__dirname, 'ssl/client.crt')
617
, keyFile = path.resolve(__dirname, 'ssl/client.key')
618
, request = require('request');
619
620
var options = {
621
url: 'https://api.some-server.com/',
622
agentOptions: {
623
cert: fs.readFileSync(certFile),
624
key: fs.readFileSync(keyFile),
625
// Or use `pfx` property replacing `cert` and `key` when using private key, certificate and CA certs in PFX or PKCS12 format:
626
// pfx: fs.readFileSync(pfxFilePath),
627
passphrase: 'password',
628
securityOptions: 'SSL_OP_NO_SSLv3'
629
}
630
};
631
632
request.get(options);
633
```
634
635
It is able to force using SSLv3 only by specifying `secureProtocol`:
636
637
```js
638
request.get({
639
url: 'https://api.some-server.com/',
640
agentOptions: {
641
secureProtocol: 'SSLv3_method'
642
}
643
});
644
```
645
646
It is possible to accept other certificates than those signed by generally allowed Certificate Authorities (CAs).
647
This can be useful, for example, when using self-signed certificates.
648
To allow a different certificate, you can specify the signing CA by adding the contents of the CA's certificate file to the `agentOptions`:
649
650
```js
651
request.get({
652
url: 'https://api.some-server.com/',
653
agentOptions: {
654
ca: fs.readFileSync('ca.cert.pem')
655
}
656
});
657
```
658
659
[back to top](#table-of-contents)
660
661
662
---
663
664
## Support for HAR 1.2
665
666
The `options.har` property will override the values: `url`, `method`, `qs`, `headers`, `form`, `formData`, `body`, `json`, as well as construct multipart data and read files from disk when `request.postData.params[].fileName` is present without a matching `value`.
667
668
a validation step will check if the HAR Request format matches the latest spec (v1.2) and will skip parsing if not matching.
669
670
```js
671
var request = require('request')
672
request({
673
// will be ignored
674
method: 'GET'
675
uri: 'http://www.google.com',
676
677
// HTTP Archive Request Object
678
har: {
679
url: 'http://www.mockbin.com/har'
680
method: 'POST',
681
headers: [
682
{
683
name: 'content-type',
684
value: 'application/x-www-form-urlencoded'
685
}
686
],
687
postData: {
688
mimeType: 'application/x-www-form-urlencoded',
689
params: [
690
{
691
name: 'foo',
692
value: 'bar'
693
},
694
{
695
name: 'hello',
696
value: 'world'
697
}
698
]
699
}
700
}
701
})
702
703
// a POST request will be sent to http://www.mockbin.com
704
// with body an application/x-www-form-urlencoded body:
705
// foo=bar&hello=world
706
```
707
708
[back to top](#table-of-contents)
709
710
711
---
712
713
## request(options, callback)
714
715
The first argument can be either a `url` or an `options` object. The only required option is `uri`; all others are optional.
716
717
- `uri` || `url` - fully qualified uri or a parsed url object from `url.parse()`
718
- `baseUrl` - fully qualified uri string used as the base url. Most useful with `request.defaults`, for example when you want to do many requests to the same domain. If `baseUrl` is `https://example.com/api/`, then requesting `/end/point?test=true` will fetch `https://example.com/api/end/point?test=true`. When `baseUrl` is given, `uri` must also be a string.
719
- `method` - http method (default: `"GET"`)
720
- `headers` - http headers (default: `{}`)
721
722
---
723
724
- `qs` - object containing querystring values to be appended to the `uri`
725
- `qsParseOptions` - object containing options to pass to the [qs.parse](https://github.com/hapijs/qs#parsing-objects) method. Alternatively pass options to the [querystring.parse](https://nodejs.org/docs/v0.12.0/api/querystring.html#querystring_querystring_parse_str_sep_eq_options) method using this format `{sep:';', eq:':', options:{}}`
726
- `qsStringifyOptions` - object containing options to pass to the [qs.stringify](https://github.com/hapijs/qs#stringifying) method. Alternatively pass options to the [querystring.stringify](https://nodejs.org/docs/v0.12.0/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options) method using this format `{sep:';', eq:':', options:{}}`. For example, to change the way arrays are converted to query strings using the `qs` module pass the `arrayFormat` option with one of `indices|brackets|repeat`
727
- `useQuerystring` - If true, use `querystring` to stringify and parse
728
querystrings, otherwise use `qs` (default: `false`). Set this option to
729
`true` if you need arrays to be serialized as `foo=bar&foo=baz` instead of the
730
default `foo[0]=bar&foo[1]=baz`.
731
732
---
733
734
- `body` - entity body for PATCH, POST and PUT requests. Must be a `Buffer` or `String`, unless `json` is `true`. If `json` is `true`, then `body` must be a JSON-serializable object.
735
- `form` - when passed an object or a querystring, this sets `body` to a querystring representation of value, and adds `Content-type: application/x-www-form-urlencoded` header. When passed no options, a `FormData` instance is returned (and is piped to request). See "Forms" section above.
736
- `formData` - Data to pass for a `multipart/form-data` request. See
737
[Forms](#forms) section above.
738
- `multipart` - array of objects which contain their own headers and `body`
739
attributes. Sends a `multipart/related` request. See [Forms](#forms) section
740
above.
741
- Alternatively you can pass in an object `{chunked: false, data: []}` where
742
`chunked` is used to specify whether the request is sent in
743
[chunked transfer encoding](https://en.wikipedia.org/wiki/Chunked_transfer_encoding)
744
In non-chunked requests, data items with body streams are not allowed.
745
- `preambleCRLF` - append a newline/CRLF before the boundary of your `multipart/form-data` request.
746
- `postambleCRLF` - append a newline/CRLF at the end of the boundary of your `multipart/form-data` request.
747
- `json` - sets `body` but to JSON representation of value and adds `Content-type: application/json` header. Additionally, parses the response body as JSON.
748
- `jsonReviver` - a [reviver function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) that will be passed to `JSON.parse()` when parsing a JSON response body.
749
750
---
751
752
- `auth` - A hash containing values `user` || `username`, `pass` || `password`, and `sendImmediately` (optional). See documentation above.
753
- `oauth` - Options for OAuth HMAC-SHA1 signing. See documentation above.
754
- `hawk` - Options for [Hawk signing](https://github.com/hueniverse/hawk). The `credentials` key must contain the necessary signing info, [see hawk docs for details](https://github.com/hueniverse/hawk#usage-example).
755
- `aws` - `object` containing AWS signing information. Should have the properties `key`, `secret`. Also requires the property `bucket`, unless you’re specifying your `bucket` as part of the path, or the request doesn’t use a bucket (i.e. GET Services)
756
- `httpSignature` - Options for the [HTTP Signature Scheme](https://github.com/joyent/node-http-signature/blob/master/http_signing.md) using [Joyent's library](https://github.com/joyent/node-http-signature). The `keyId` and `key` properties must be specified. See the docs for other options.
757
758
---
759
760
- `followRedirect` - follow HTTP 3xx responses as redirects (default: `true`). This property can also be implemented as function which gets `response` object as a single argument and should return `true` if redirects should continue or `false` otherwise.
761
- `followAllRedirects` - follow non-GET HTTP 3xx responses as redirects (default: `false`)
762
- `maxRedirects` - the maximum number of redirects to follow (default: `10`)
763
- `removeRefererHeader` - removes the referer header when a redirect happens (default: `false`).
764
765
---
766
767
- `encoding` - Encoding to be used on `setEncoding` of response data. If `null`, the `body` is returned as a `Buffer`. Anything else **(including the default value of `undefined`)** will be passed as the [encoding](http://nodejs.org/api/buffer.html#buffer_buffer) parameter to `toString()` (meaning this is effectively `utf8` by default).
768
- `gzip` - If `true`, add an `Accept-Encoding` header to request compressed content encodings from the server (if not already present) and decode supported content encodings in the response. **Note:** Automatic decoding of the response content is performed on the body data returned through `request` (both through the `request` stream and passed to the callback function) but is not performed on the `response` stream (available from the `response` event) which is the unmodified `http.IncomingMessage` object which may contain compressed data. See example below.
769
- `jar` - If `true` and `tough-cookie` is installed, remember cookies for future use (or define your custom cookie jar; see examples section)
770
771
---
772
773
- `pool` - An object describing which agents to use for the request. If this option is omitted the request will use the global agent (as long as [your options allow for it](request.js#L747)). Otherwise, request will search the pool for your custom agent. If no custom agent is found, a new agent will be created and added to the pool.
774
- A `maxSockets` property can also be provided on the `pool` object to set the max number of sockets for all agents created (ex: `pool: {maxSockets: Infinity}`).
775
- Note that if you are sending multiple requests in a loop and creating
776
multiple new `pool` objects, `maxSockets` will not work as intended. To
777
work around this, either use [`request.defaults`](#requestdefaultsoptions)
778
with your pool options or create the pool object with the `maxSockets`
779
property outside of the loop.
780
- `timeout` - Integer containing the number of milliseconds to wait for a
781
request to respond before aborting the request. Note that if the underlying
782
TCP connection cannot be established, the OS-wide TCP connection timeout will
783
overrule the `timeout` option ([the default in Linux is around 20 seconds](http://www.sekuda.com/overriding_the_default_linux_kernel_20_second_tcp_socket_connect_timeout)).
784
- `localAddress` - Local interface to bind for network connections.
785
- `proxy` - An HTTP proxy to be used. Supports proxy Auth with Basic Auth, identical to support for the `url` parameter (by embedding the auth info in the `uri`)
786
- `strictSSL` - If `true`, requires SSL certificates be valid. **Note:** to use your own certificate authority, you need to specify an agent that was created with that CA as an option.
787
- `agentOptions` - Object containing user agent options. See documentation above. **Note:** [see tls API doc for TLS/SSL options](http://nodejs.org/api/tls.html#tls_tls_connect_options_callback).
788
- `tunnel` - controls the behavior of
789
[HTTP `CONNECT` tunneling](https://en.wikipedia.org/wiki/HTTP_tunnel#HTTP_CONNECT_tunneling)
790
as follows:
791
- `undefined` (default) - `true` if the destination is `https` or a previous
792
request in the redirect chain used a tunneling proxy, `false` otherwise
793
- `true` - always tunnel to the destination by making a `CONNECT` request to
794
the proxy
795
- `false` - request the destination as a `GET` request.
796
- `proxyHeaderWhiteList` - A whitelist of headers to send to a
797
tunneling proxy.
798
- `proxyHeaderExclusiveList` - A whitelist of headers to send
799
exclusively to a tunneling proxy and not to destination.
800
801
---
802
803
- `time` - If `true`, the request-response cycle (including all redirects) is timed at millisecond resolution, and the result provided on the response's `elapsedTime` property.
804
805
---
806
807
- `har` - A [HAR 1.2 Request Object](http://www.softwareishard.com/blog/har-12-spec/#request), will be processed from HAR format into options overwriting matching values *(see the [HAR 1.2 section](#support-for-har-1.2) for details)*
808
809
The callback argument gets 3 arguments:
810
811
1. An `error` when applicable (usually from [`http.ClientRequest`](http://nodejs.org/api/http.html#http_class_http_clientrequest) object)
812
2. An [`http.IncomingMessage`](http://nodejs.org/api/http.html#http_http_incomingmessage) object
813
3. The third is the `response` body (`String` or `Buffer`, or JSON object if the `json` option is supplied)
814
815
[back to top](#table-of-contents)
816
817
818
---
819
820
## Convenience methods
821
822
There are also shorthand methods for different HTTP METHODs and some other conveniences.
823
824
825
### request.defaults(options)
826
827
This method **returns a wrapper** around the normal request API that defaults
828
to whatever options you pass to it.
829
830
**Note:** `request.defaults()` **does not** modify the global request API;
831
instead, it **returns a wrapper** that has your default settings applied to it.
832
833
**Note:** You can call `.defaults()` on the wrapper that is returned from
834
`request.defaults` to add/override defaults that were previously defaulted.
835
836
For example:
837
```js
838
//requests using baseRequest() will set the 'x-token' header
839
var baseRequest = request.defaults({
840
headers: {x-token: 'my-token'}
841
})
842
843
//requests using specialRequest() will include the 'x-token' header set in
844
//baseRequest and will also include the 'special' header
845
var specialRequest = baseRequest.defaults({
846
headers: {special: 'special value'}
847
})
848
```
849
850
### request.put
851
852
Same as `request()`, but defaults to `method: "PUT"`.
853
854
```js
855
request.put(url)
856
```
857
858
### request.patch
859
860
Same as `request()`, but defaults to `method: "PATCH"`.
861
862
```js
863
request.patch(url)
864
```
865
866
### request.post
867
868
Same as `request()`, but defaults to `method: "POST"`.
869
870
```js
871
request.post(url)
872
```
873
874
### request.head
875
876
Same as `request()`, but defaults to `method: "HEAD"`.
877
878
```js
879
request.head(url)
880
```
881
882
### request.del
883
884
Same as `request()`, but defaults to `method: "DELETE"`.
885
886
```js
887
request.del(url)
888
```
889
890
### request.get
891
892
Same as `request()` (for uniformity).
893
894
```js
895
request.get(url)
896
```
897
### request.cookie
898
899
Function that creates a new cookie.
900
901
```js
902
request.cookie('key1=value1')
903
```
904
### request.jar()
905
906
Function that creates a new cookie jar.
907
908
```js
909
request.jar()
910
```
911
912
[back to top](#table-of-contents)
913
914
915
---
916
917
918
## Debugging
919
920
There are at least three ways to debug the operation of `request`:
921
922
1. Launch the node process like `NODE_DEBUG=request node script.js`
923
(`lib,request,otherlib` works too).
924
925
2. Set `require('request').debug = true` at any time (this does the same thing
926
as #1).
927
928
3. Use the [request-debug module](https://github.com/nylen/request-debug) to
929
view request and response headers and bodies.
930
931
[back to top](#table-of-contents)
932
933
934
---
935
936
937
## Examples:
938
939
```js
940
var request = require('request')
941
, rand = Math.floor(Math.random()*100000000).toString()
942
;
943
request(
944
{ method: 'PUT'
945
, uri: 'http://mikeal.iriscouch.com/testjs/' + rand
946
, multipart:
947
[ { 'content-type': 'application/json'
948
, body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
949
}
950
, { body: 'I am an attachment' }
951
]
952
}
953
, function (error, response, body) {
954
if(response.statusCode == 201){
955
console.log('document saved as: http://mikeal.iriscouch.com/testjs/'+ rand)
956
} else {
957
console.log('error: '+ response.statusCode)
958
console.log(body)
959
}
960
}
961
)
962
```
963
964
For backwards-compatibility, response compression is not supported by default.
965
To accept gzip-compressed responses, set the `gzip` option to `true`. Note
966
that the body data passed through `request` is automatically decompressed
967
while the response object is unmodified and will contain compressed data if
968
the server sent a compressed response.
969
970
```js
971
var request = require('request')
972
request(
973
{ method: 'GET'
974
, uri: 'http://www.google.com'
975
, gzip: true
976
}
977
, function (error, response, body) {
978
// body is the decompressed response body
979
console.log('server encoded the data as: ' + (response.headers['content-encoding'] || 'identity'))
980
console.log('the decoded data is: ' + body)
981
}
982
).on('data', function(data) {
983
// decompressed data as it is received
984
console.log('decoded chunk: ' + data)
985
})
986
.on('response', function(response) {
987
// unmodified http.IncomingMessage object
988
response.on('data', function(data) {
989
// compressed data as it is received
990
console.log('received ' + data.length + ' bytes of compressed data')
991
})
992
})
993
```
994
995
Cookies are disabled by default (else, they would be used in subsequent requests). To enable cookies, set `jar` to `true` (either in `defaults` or `options`) and install `tough-cookie`.
996
997
```js
998
var request = request.defaults({jar: true})
999
request('http://www.google.com', function () {
1000
request('http://images.google.com')
1001
})
1002
```
1003
1004
To use a custom cookie jar (instead of `request`’s global cookie jar), set `jar` to an instance of `request.jar()` (either in `defaults` or `options`)
1005
1006
```js
1007
var j = request.jar()
1008
var request = request.defaults({jar:j})
1009
request('http://www.google.com', function () {
1010
request('http://images.google.com')
1011
})
1012
```
1013
1014
OR
1015
1016
```js
1017
var j = request.jar();
1018
var cookie = request.cookie('key1=value1');
1019
var url = 'http://www.google.com';
1020
j.setCookie(cookie, url);
1021
request({url: url, jar: j}, function () {
1022
request('http://images.google.com')
1023
})
1024
```
1025
1026
To use a custom cookie store (such as a
1027
[`FileCookieStore`](https://github.com/mitsuru/tough-cookie-filestore)
1028
which supports saving to and restoring from JSON files), pass it as a parameter
1029
to `request.jar()`:
1030
1031
```js
1032
var FileCookieStore = require('tough-cookie-filestore');
1033
// NOTE - currently the 'cookies.json' file must already exist!
1034
var j = request.jar(new FileCookieStore('cookies.json'));
1035
request = request.defaults({ jar : j })
1036
request('http://www.google.com', function() {
1037
request('http://images.google.com')
1038
})
1039
```
1040
1041
The cookie store must be a
1042
[`tough-cookie`](https://github.com/goinstant/tough-cookie)
1043
store and it must support synchronous operations; see the
1044
[`CookieStore` API docs](https://github.com/goinstant/tough-cookie/#cookiestore-api)
1045
for details.
1046
1047
To inspect your cookie jar after a request:
1048
1049
```js
1050
var j = request.jar()
1051
request({url: 'http://www.google.com', jar: j}, function () {
1052
var cookie_string = j.getCookieString(uri); // "key1=value1; key2=value2; ..."
1053
var cookies = j.getCookies(uri);
1054
// [{key: 'key1', value: 'value1', domain: "www.google.com", ...}, ...]
1055
})
1056
```
1057
1058
[back to top](#table-of-contents)
1059