Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 1064 lines (815 sloc) 39.382 kB
28e957c @simov Add table of contents in readme
simov authored
1
2 # Request - Simplified HTTP client
3
0d4c332 @nylen Clean up readme badges, and add Travis and Coveralls badges
nylen authored
4 [![npm package](https://nodei.co/npm/request.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/request/)
02f6b38 @Stanley Added specs for body option
Stanley authored
5
28e957c @simov Add table of contents in readme
simov authored
6 [![Build status](https://img.shields.io/travis/request/request.svg?style=flat-square)](https://travis-ci.org/request/request)
9701a3c @simov Return back coveralls, clean npm scripts
simov authored
7 [![Coverage](https://img.shields.io/codecov/c/github/request/request.svg?style=flat-square)](https://codecov.io/github/request/request?branch=master)
28e957c @simov Add table of contents in readme
simov authored
8 [![Coverage](https://img.shields.io/coveralls/request/request.svg?style=flat-square)](https://coveralls.io/r/request/request)
38c4428 @YasharF Updating the dependency status bar in README.md
YasharF authored
9 [![Dependency Status](https://img.shields.io/david/request/request.svg?style=flat-square)](https://david-dm.org/request/request)
28e957c @simov Add table of contents in readme
simov authored
10 [![Gitter](https://img.shields.io/badge/gitter-join_chat-blue.svg?style=flat-square)](https://gitter.im/request/request?utm_source=badge)
11
ed47d6b @nylen Move from mikeal/request to request/request
nylen authored
12
12a4cb8 @mikeal Matching node documentation style.
mikeal authored
13 ## Super simple to use
758f598 @mikeal Initial commit. Request package.
mikeal authored
14
f4b87cf @TehShrike I'm not OCD seriously
TehShrike authored
15 Request is designed to be the simplest way possible to make http calls. It supports HTTPS and follows redirects by default.
4ff3493 @mikeal Updates to README.md
mikeal authored
16
28e957c @simov Add table of contents in readme
simov authored
17 ```js
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
18 var request = require('request');
19 request('http://www.google.com', function (error, response, body) {
20 if (!error && response.statusCode == 200) {
368f3e3 @nylen Fix typo
nylen authored
21 console.log(body) // Show the HTML for the Google homepage.
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
22 }
23 })
24 ```
25
28e957c @simov Add table of contents in readme
simov authored
26
27 ## Table of contents
28
29 - [Streaming](#streaming)
30 - [Forms](#forms)
31 - [HTTP Authentication](#http-authentication)
32 - [Custom HTTP Headers](#custom-http-headers)
33 - [OAuth Signing](#oauth-signing)
34 - [Proxies](#proxies)
35 - [Unix Domain Sockets](#unix-domain-sockets)
36 - [TLS/SSL Protocol](#tlsssl-protocol)
a970d68 @ahmadnassri fix TOC link
ahmadnassri authored
37 - [Support for HAR 1.2](#support-for-har-12)
28e957c @simov Add table of contents in readme
simov authored
38 - [**All Available Options**](#requestoptions-callback)
39
40 Request also offers [convenience methods](#convenience-methods) like
41 `request.defaults` and `request.post`, and there are
42 lots of [usage examples](#examples) and several
43 [debugging techniques](#debugging).
44
45
46 ---
47
48
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
49 ## Streaming
50
51 You can stream any response to a file stream.
52
28e957c @simov Add table of contents in readme
simov authored
53 ```js
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
54 request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))
55 ```
56
9072ff1 Edited README.md for formatting and clarity of phrasing
Tony authored
57 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).
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
58
28e957c @simov Add table of contents in readme
simov authored
59 ```js
e77a169 @mikeal fixing streaming example. fixes #164
mikeal authored
60 fs.createReadStream('file.json').pipe(request.put('http://mysite.com/obj.json'))
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
61 ```
62
9072ff1 Edited README.md for formatting and clarity of phrasing
Tony authored
63 Request can also `pipe` to itself. When doing so, `content-type` and `content-length` are preserved in the PUT headers.
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
64
28e957c @simov Add table of contents in readme
simov authored
65 ```js
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
66 request.get('http://google.com/img.png').pipe(request.put('http://mysite.com/img.png'))
67 ```
68
98551bf @tbuchok specifically mentions http.IncomingMessage
tbuchok authored
69 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).
8c1077e @tbuchok adds documentation for the "response" event within the streaming section
tbuchok authored
70
28e957c @simov Add table of contents in readme
simov authored
71 ```js
8c1077e @tbuchok adds documentation for the "response" event within the streaming section
tbuchok authored
72 request
73 .get('http://google.com/img.png')
74 .on('response', function(response) {
75 console.log(response.statusCode) // 200
76 console.log(response.headers['content-type']) // 'image/png'
77 })
78 .pipe(request.put('http://mysite.com/img.png'))
79 ```
80
dd77511 @vmattos Documenting error handling when usnig streams
vmattos authored
81 To easily handle errors when streaming requests, listen to the `error` event before piping:
82
28e957c @simov Add table of contents in readme
simov authored
83 ```js
dd77511 @vmattos Documenting error handling when usnig streams
vmattos authored
84 request
85 .get('http://mysite.com/doodle.png')
86 .on('error', function(err) {
f8c7491 @vmattos Avoiding confusion
vmattos authored
87 console.log(err)
dd77511 @vmattos Documenting error handling when usnig streams
vmattos authored
88 })
89 .pipe(fs.createWriteStream('doodle.png'))
90 ```
91
9072ff1 Edited README.md for formatting and clarity of phrasing
Tony authored
92 Now let’s get fancy.
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
93
28e957c @simov Add table of contents in readme
simov authored
94 ```js
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
95 http.createServer(function (req, resp) {
96 if (req.url === '/doodle.png') {
97 if (req.method === 'PUT') {
98 req.pipe(request.put('http://mysite.com/doodle.png'))
99 } else if (req.method === 'GET' || req.method === 'HEAD') {
100 request.get('http://mysite.com/doodle.png').pipe(resp)
f98ff99 @mikeal Update this old ass readme with some new HOTNESS!
mikeal authored
101 }
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
102 }
103 })
104 ```
105
9072ff1 Edited README.md for formatting and clarity of phrasing
Tony authored
106 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:
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
107
28e957c @simov Add table of contents in readme
simov authored
108 ```js
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
109 http.createServer(function (req, resp) {
110 if (req.url === '/doodle.png') {
111 var x = request('http://mysite.com/doodle.png')
112 req.pipe(x)
113 x.pipe(resp)
114 }
115 })
116 ```
117
9072ff1 Edited README.md for formatting and clarity of phrasing
Tony authored
118 And since `pipe()` returns the destination stream in ≥ Node 0.5.x you can do one line proxying. :)
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
119
28e957c @simov Add table of contents in readme
simov authored
120 ```js
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
121 req.pipe(request('http://mysite.com/doodle.png')).pipe(resp)
122 ```
123
124 Also, none of this new functionality conflicts with requests previous features, it just expands them.
125
28e957c @simov Add table of contents in readme
simov authored
126 ```js
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
127 var r = request.defaults({'proxy':'http://localproxy.com'})
128
129 http.createServer(function (req, resp) {
130 if (req.url === '/doodle.png') {
131 r.get('http://google.com/doodle.png').pipe(resp)
132 }
133 })
134 ```
9072ff1 Edited README.md for formatting and clarity of phrasing
Tony authored
135
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
136 You can still use intermediate proxies, the requests will still follow HTTP forwards, etc.
d67a041 @mikeal Moving basic example to the top.
mikeal authored
137
28e957c @simov Add table of contents in readme
simov authored
138 [back to top](#table-of-contents)
d9148f1 @isaacs readme: clarify that proxy-authorization is NEVER sent to the endpoint
isaacs authored
139
fb206a0 @samcday Add some documentation on https?_proxy + no_proxy.
samcday authored
140
28e957c @simov Add table of contents in readme
simov authored
141 ---
ccfec0b @FredKSchott Support Smarter Unix URL Scheme
FredKSchott authored
142
143
4f51cec @mikeal Docs for the form API, pumping version.
mikeal authored
144 ## Forms
145
90245d7 @mikeal Doc fixes.
mikeal authored
146 `request` supports `application/x-www-form-urlencoded` and `multipart/form-data` form uploads. For `multipart/related` refer to the `multipart` API.
4f51cec @mikeal Docs for the form API, pumping version.
mikeal authored
147
28e957c @simov Add table of contents in readme
simov authored
148
d8bf080 update form documentation for new usage
Fred K. Schott authored
149 #### application/x-www-form-urlencoded (URL-Encoded Forms)
150
9072ff1 Edited README.md for formatting and clarity of phrasing
Tony authored
151 URL-encoded forms are simple.
4f51cec @mikeal Docs for the form API, pumping version.
mikeal authored
152
28e957c @simov Add table of contents in readme
simov authored
153 ```js
4f51cec @mikeal Docs for the form API, pumping version.
mikeal authored
154 request.post('http://service.com/upload', {form:{key:'value'}})
155 // or
156 request.post('http://service.com/upload').form({key:'value'})
b39644f @MiroRadenovic in post can send form data and use callback
MiroRadenovic authored
157 // or
158 request.post({url:'http://service.com/upload', form: {key:'value'}}, function(err,httpResponse,body){ /* ... */ })
4f51cec @mikeal Docs for the form API, pumping version.
mikeal authored
159 ```
160
28e957c @simov Add table of contents in readme
simov authored
161
d8bf080 update form documentation for new usage
Fred K. Schott authored
162 #### multipart/form-data (Multipart Form Uploads)
163
164 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.
daaa5fb @crocket Add support for multipart form data in request options.
crocket authored
165
166
28e957c @simov Add table of contents in readme
simov authored
167 ```js
daaa5fb @crocket Add support for multipart form data in request options.
crocket authored
168 var formData = {
d8bf080 update form documentation for new usage
Fred K. Schott authored
169 // Pass a simple key-value pair
daaa5fb @crocket Add support for multipart form data in request options.
crocket authored
170 my_field: 'my_value',
d8bf080 update form documentation for new usage
Fred K. Schott authored
171 // Pass data via Buffers
daaa5fb @crocket Add support for multipart form data in request options.
crocket authored
172 my_buffer: new Buffer([1, 2, 3]),
d8bf080 update form documentation for new usage
Fred K. Schott authored
173 // Pass data via Streams
daaa5fb @crocket Add support for multipart form data in request options.
crocket authored
174 my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
d8bf080 update form documentation for new usage
Fred K. Schott authored
175 // Pass multiple values /w an Array
1a742c1 @dotcypress add unit tests
dotcypress authored
176 attachments: [
1b9babc @LewisJEllis Fix typo in README.md
LewisJEllis authored
177 fs.createReadStream(__dirname + '/attachment1.jpg'),
1a742c1 @dotcypress add unit tests
dotcypress authored
178 fs.createReadStream(__dirname + '/attachment2.jpg')
179 ],
d8bf080 update form documentation for new usage
Fred K. Schott authored
180 // Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS}
068ab7d @cliffcrosland Update README.md to explain custom file use case
cliffcrosland authored
181 // Use case: for some types of streams, you'll need to provide "file"-related information manually.
d8bf080 update form documentation for new usage
Fred K. Schott authored
182 // See the `form-data` README for more information about options: https://github.com/felixge/node-form-data
669200a @dotcypress add metadata for form-data file field
dotcypress authored
183 custom_file: {
184 value: fs.createReadStream('/dev/urandom'),
185 options: {
186 filename: 'topsecret.jpg',
187 contentType: 'image/jpg'
188 }
189 }
daaa5fb @crocket Add support for multipart form data in request options.
crocket authored
190 };
bad6770 @crocket Rename formdata to formData
crocket authored
191 request.post({url:'http://service.com/upload', formData: formData}, function optionalCallback(err, httpResponse, body) {
daaa5fb @crocket Add support for multipart form data in request options.
crocket authored
192 if (err) {
193 return console.error('upload failed:', err);
194 }
195 console.log('Upload successful! Server responded with:', body);
196 });
197 ```
198
1b9babc @LewisJEllis Fix typo in README.md
LewisJEllis authored
199 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.)
4f51cec @mikeal Docs for the form API, pumping version.
mikeal authored
200
28e957c @simov Add table of contents in readme
simov authored
201 ```js
d8bf080 update form documentation for new usage
Fred K. Schott authored
202 // NOTE: Advanced use-case, for normal use see 'formData' usage above
5cdcbe5 @simov Add docs and tests for passing options to the querystring's
simov authored
203 var r = request.post('http://service.com/upload', function optionalCallback(err, httpResponse, body) {...})
daaa5fb @crocket Add support for multipart form data in request options.
crocket authored
204 var form = r.form();
205 form.append('my_field', 'my_value');
206 form.append('my_buffer', new Buffer([1, 2, 3]));
d8bf080 update form documentation for new usage
Fred K. Schott authored
207 form.append('custom_file', fs.createReadStream(__dirname + '/unicycle.jpg'), {filename: 'unicycle.jpg'});
4f51cec @mikeal Docs for the form API, pumping version.
mikeal authored
208 ```
d8bf080 update form documentation for new usage
Fred K. Schott authored
209 See the [form-data README](https://github.com/felixge/node-form-data) for more information & examples.
210
28e957c @simov Add table of contents in readme
simov authored
211
d8bf080 update form documentation for new usage
Fred K. Schott authored
212 #### multipart/related
4f51cec @mikeal Docs for the form API, pumping version.
mikeal authored
213
292537f @netpoetica clearer and more accurate language
netpoetica authored
214 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.
b4e3241 @netpoetica add documentation for preambleCRLF and postambleCRLF to options list …
netpoetica authored
215
28e957c @simov Add table of contents in readme
simov authored
216 ```js
5dd3ff1 @simov Improve multipart docs
simov authored
217 request({
218 method: 'PUT',
219 preambleCRLF: true,
220 postambleCRLF: true,
221 uri: 'http://service.com/upload',
222 multipart: [
223 {
224 'content-type': 'application/json'
225 body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
226 },
227 { body: 'I am an attachment' },
228 { body: fs.createReadStream('image.png') }
229 ],
230 // alternatively pass an object containing additional options
231 multipart: {
232 chunked: false,
233 data: [
234 {
1b9babc @LewisJEllis Fix typo in README.md
LewisJEllis authored
235 'content-type': 'application/json',
5dd3ff1 @simov Improve multipart docs
simov authored
236 body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
237 },
238 { body: 'I am an attachment' }
b4e3241 @netpoetica add documentation for preambleCRLF and postambleCRLF to options list …
netpoetica authored
239 ]
240 }
5dd3ff1 @simov Improve multipart docs
simov authored
241 },
242 function (error, response, body) {
243 if (error) {
244 return console.error('upload failed:', error);
b4e3241 @netpoetica add documentation for preambleCRLF and postambleCRLF to options list …
netpoetica authored
245 }
5dd3ff1 @simov Improve multipart docs
simov authored
246 console.log('Upload successful! Server responded with:', body);
247 })
b4e3241 @netpoetica add documentation for preambleCRLF and postambleCRLF to options list …
netpoetica authored
248 ```
249
28e957c @simov Add table of contents in readme
simov authored
250 [back to top](#table-of-contents)
251
252
253 ---
254
b4e3241 @netpoetica add documentation for preambleCRLF and postambleCRLF to options list …
netpoetica authored
255
85fd359 @nylen Document new auth options
nylen authored
256 ## HTTP Authentication
257
28e957c @simov Add table of contents in readme
simov authored
258 ```js
a918e04 @tschaub The exported request function doesn't have an auth method
tschaub authored
259 request.get('http://some.server.com/').auth('username', 'password', false);
85fd359 @nylen Document new auth options
nylen authored
260 // or
261 request.get('http://some.server.com/', {
262 'auth': {
263 'user': 'username',
264 'pass': 'password',
265 'sendImmediately': false
266 }
267 });
ba51a26 @phedny Add documentation about auth.bearer
phedny authored
268 // or
269 request.get('http://some.server.com/').auth(null, null, true, 'bearerToken');
270 // or
271 request.get('http://some.server.com/', {
272 'auth': {
273 'bearer': 'bearerToken'
274 }
275 });
85fd359 @nylen Document new auth options
nylen authored
276 ```
277
9206260 @nylen Improve auth docs
nylen authored
278 If passed as an option, `auth` should be a hash containing values:
85fd359 @nylen Document new auth options
nylen authored
279
9206260 @nylen Improve auth docs
nylen authored
280 - `user` || `username`
281 - `pass` || `password`
282 - `sendImmediately` (optional)
283 - `bearer` (optional)
85fd359 @nylen Document new auth options
nylen authored
284
9206260 @nylen Improve auth docs
nylen authored
285 The method form takes parameters
286 `auth(username, password, sendImmediately, bearer)`.
287
288 `sendImmediately` defaults to `true`, which causes a basic or bearer
289 authentication header to be sent. If `sendImmediately` is `false`, then
290 `request` will retry with a proper authentication header after receiving a
291 `401` response from the server (which must contain a `WWW-Authenticate` header
292 indicating the required authentication method).
293
294 Note that you can also specify basic authentication using the URL itself, as
295 detailed in [RFC 1738](http://www.ietf.org/rfc/rfc1738.txt). Simply pass the
296 `user:password` before the host with an `@` sign:
25ce9a1 @pvoznenko Update README.md
pvoznenko authored
297
28e957c @simov Add table of contents in readme
simov authored
298 ```js
032a8f2 @pvoznenko Update README.md
pvoznenko authored
299 var username = 'username',
300 password = 'password',
301 url = 'http://' + username + ':' + password + '@some.server.com';
25ce9a1 @pvoznenko Update README.md
pvoznenko authored
302
303 request({url: url}, function (error, response, body) {
304 // Do more stuff with 'body' here
305 });
306 ```
307
9206260 @nylen Improve auth docs
nylen authored
308 Digest authentication is supported, but it only works with `sendImmediately`
309 set to `false`; otherwise `request` will send basic authentication on the
310 initial request, which will probably cause the request to fail.
032a8f2 @pvoznenko Update README.md
pvoznenko authored
311
9206260 @nylen Improve auth docs
nylen authored
312 Bearer authentication is supported, and is activated when the `bearer` value is
313 available. The value may be either a `String` or a `Function` returning a
314 `String`. Using a function to supply the bearer token is particularly useful if
315 used in conjuction with `defaults` to allow a single function to supply the
316 last known token at the time of sending a request, or to compute one on the fly.
032a8f2 @pvoznenko Update README.md
pvoznenko authored
317
28e957c @simov Add table of contents in readme
simov authored
318 [back to top](#table-of-contents)
319
320
321 ---
322
323
324 ## Custom HTTP Headers
325
326 HTTP Headers, such as `User-Agent`, can be set in the `options` object.
327 In the example below, we call the github API to find out the number
328 of stars and forks for the request repository. This requires a
329 custom `User-Agent` header as well as https.
330
331 ```js
332 var request = require('request');
333
334 var options = {
335 url: 'https://api.github.com/repos/request/request',
336 headers: {
337 'User-Agent': 'request'
338 }
339 };
340
341 function callback(error, response, body) {
342 if (!error && response.statusCode == 200) {
343 var info = JSON.parse(body);
344 console.log(info.stargazers_count + " Stars");
345 console.log(info.forks_count + " Forks");
346 }
347 }
348
349 request(options, callback);
350 ```
351
352 [back to top](#table-of-contents)
353
354
355 ---
356
357
668a291 @mikeal Adding some documentation for OAuth signing support.
mikeal authored
358 ## OAuth Signing
359
64b8648 @nylen Update OAuth documentation for RSA-SHA1 signing
nylen authored
360 [OAuth version 1.0](https://tools.ietf.org/html/rfc5849) is supported. The
361 default signing algorithm is
362 [HMAC-SHA1](https://tools.ietf.org/html/rfc5849#section-3.4.2):
363
28e957c @simov Add table of contents in readme
simov authored
364 ```js
c0d4e21 @simov Improve OAuth1.0 server side flow example
simov authored
365 // OAuth1.0 - 3-legged server side flow (Twitter example)
366 // step 1
668a291 @mikeal Adding some documentation for OAuth signing support.
mikeal authored
367 var qs = require('querystring')
368 , oauth =
04faa3b @mikeal Minor improvements in example
mikeal authored
369 { callback: 'http://mysite.com/callback/'
668a291 @mikeal Adding some documentation for OAuth signing support.
mikeal authored
370 , consumer_key: CONSUMER_KEY
04faa3b @mikeal Minor improvements in example
mikeal authored
371 , consumer_secret: CONSUMER_SECRET
668a291 @mikeal Adding some documentation for OAuth signing support.
mikeal authored
372 }
04faa3b @mikeal Minor improvements in example
mikeal authored
373 , url = 'https://api.twitter.com/oauth/request_token'
668a291 @mikeal Adding some documentation for OAuth signing support.
mikeal authored
374 ;
375 request.post({url:url, oauth:oauth}, function (e, r, body) {
ed68b8d @joemccann Updated the twitter oauth dance. The comments weren't clear. Also rem…
joemccann authored
376 // Ideally, you would take the body in the response
377 // and construct a URL that a user clicks on (like a sign in button).
f98ff99 @mikeal Update this old ass readme with some new HOTNESS!
mikeal authored
378 // The verifier is only available in the response after a user has
ed68b8d @joemccann Updated the twitter oauth dance. The comments weren't clear. Also rem…
joemccann authored
379 // verified with twitter that they are authorizing your app.
c0d4e21 @simov Improve OAuth1.0 server side flow example
simov authored
380
381 // step 2
382 var req_data = qs.parse(body)
383 var uri = 'https://api.twitter.com/oauth/authenticate'
384 + '?' + qs.stringify({oauth_token: req_data.oauth_token})
385 // redirect the user to the authorize uri
386
387 // step 3
388 // after the user is redirected back to your server
389 var auth_data = qs.parse(body)
f98ff99 @mikeal Update this old ass readme with some new HOTNESS!
mikeal authored
390 , oauth =
668a291 @mikeal Adding some documentation for OAuth signing support.
mikeal authored
391 { consumer_key: CONSUMER_KEY
392 , consumer_secret: CONSUMER_SECRET
c0d4e21 @simov Improve OAuth1.0 server side flow example
simov authored
393 , token: auth_data.oauth_token
394 , token_secret: req_data.oauth_token_secret
395 , verifier: auth_data.oauth_verifier
668a291 @mikeal Adding some documentation for OAuth signing support.
mikeal authored
396 }
397 , url = 'https://api.twitter.com/oauth/access_token'
398 ;
399 request.post({url:url, oauth:oauth}, function (e, r, body) {
c0d4e21 @simov Improve OAuth1.0 server side flow example
simov authored
400 // ready to make signed requests on behalf of the user
401 var perm_data = qs.parse(body)
f98ff99 @mikeal Update this old ass readme with some new HOTNESS!
mikeal authored
402 , oauth =
668a291 @mikeal Adding some documentation for OAuth signing support.
mikeal authored
403 { consumer_key: CONSUMER_KEY
404 , consumer_secret: CONSUMER_SECRET
c0d4e21 @simov Improve OAuth1.0 server side flow example
simov authored
405 , token: perm_data.oauth_token
406 , token_secret: perm_data.oauth_token_secret
668a291 @mikeal Adding some documentation for OAuth signing support.
mikeal authored
407 }
c0d4e21 @simov Improve OAuth1.0 server side flow example
simov authored
408 , url = 'https://api.twitter.com/1.1/users/show.json'
409 , qs =
410 { screen_name: perm_data.screen_name
411 , user_id: perm_data.user_id
668a291 @mikeal Adding some documentation for OAuth signing support.
mikeal authored
412 }
413 ;
c6657df @simov Fix OAuth docs - closes #1632
simov authored
414 request.get({url:url, oauth:oauth, qs:qs, json:true}, function (e, r, user) {
668a291 @mikeal Adding some documentation for OAuth signing support.
mikeal authored
415 console.log(user)
416 })
417 })
418 })
419 ```
420
64b8648 @nylen Update OAuth documentation for RSA-SHA1 signing
nylen authored
421 For [RSA-SHA1 signing](https://tools.ietf.org/html/rfc5849#section-3.4.3), make
422 the following changes to the OAuth options object:
423 * Pass `signature_method : 'RSA-SHA1'`
424 * Instead of `consumer_secret`, specify a `private_key` string in
425 [PEM format](http://how2ssl.com/articles/working_with_pem_files/)
426
3bd5421 @simov Add support for OAuth plaintext signature method
simov authored
427 For [PLAINTEXT signing](http://oauth.net/core/1.0/#anchor22), make
428 the following changes to the OAuth options object:
429 * Pass `signature_method : 'PLAINTEXT'`
430
7732d64 @fantapop Added lesser used oauth transport methods body and query as described…
fantapop authored
431 To send OAuth parameters via query params or in a post body as described in The
432 [Consumer Request Parameters](http://oauth.net/core/1.0/#consumer_req_param)
433 section of the oauth1 spec:
434 * Pass `transport_method : 'query'` or `transport_method : 'body'` in the OAuth
435 options object.
436 * `transport_method` defaults to `'header'`
437
e2fa5e9 @simov Add OAuth body_hash to the docs
simov authored
438 To use [Request Body Hash](https://oauth.googlecode.com/svn/spec/ext/body_hash/1.0/oauth-bodyhash.html) you can either
439 * Manually generate the body hash and pass it as a string `body_hash: '...'`
440 * Automatically generate the body hash by passing `body_hash: true`
441
28e957c @simov Add table of contents in readme
simov authored
442 [back to top](#table-of-contents)
668a291 @mikeal Adding some documentation for OAuth signing support.
mikeal authored
443
8795fc6 @tcort README.md: add custom HTTP Headers example.
tcort authored
444
28e957c @simov Add table of contents in readme
simov authored
445 ---
8795fc6 @tcort README.md: add custom HTTP Headers example.
tcort authored
446
447
28e957c @simov Add table of contents in readme
simov authored
448 ## Proxies
8795fc6 @tcort README.md: add custom HTTP Headers example.
tcort authored
449
28e957c @simov Add table of contents in readme
simov authored
450 If you specify a `proxy` option, then the request (and any subsequent
451 redirects) will be sent via a connection to the proxy server.
452
453 If your endpoint is an `https` url, and you are using a proxy, then
454 request will send a `CONNECT` request to the proxy server *first*, and
455 then use the supplied connection to connect to the endpoint.
456
457 That is, first it will make a request like:
458
459 ```
460 HTTP/1.1 CONNECT endpoint-server.com:80
461 Host: proxy-server.com
462 User-Agent: whatever user agent you specify
463 ```
464
465 and then the proxy server make a TCP connection to `endpoint-server`
466 on port `80`, and return a response that looks like:
467
468 ```
469 HTTP/1.1 200 OK
470 ```
471
472 At this point, the connection is left open, and the client is
473 communicating directly with the `endpoint-server.com` machine.
474
475 See [the wikipedia page on HTTP Tunneling](http://en.wikipedia.org/wiki/HTTP_tunnel)
476 for more information.
477
478 By default, when proxying `http` traffic, request will simply make a
479 standard proxied `http` request. This is done by making the `url`
480 section of the initial line of the request a fully qualified url to
481 the endpoint.
482
483 For example, it will make a single request that looks like:
484
485 ```
486 HTTP/1.1 GET http://endpoint-server.com/some-url
487 Host: proxy-server.com
488 Other-Headers: all go here
489
490 request body or whatever
491 ```
492
493 Because a pure "http over http" tunnel offers no additional security
494 or other features, it is generally simpler to go with a
495 straightforward HTTP proxy in this case. However, if you would like
496 to force a tunneling proxy, you may set the `tunnel` option to `true`.
497
498 You can also make a standard proxied `http` request by explicitly setting
499 `tunnel : false`, but **note that this will allow the proxy to see the traffic
500 to/from the destination server**.
501
502 If you are using a tunneling proxy, you may set the
503 `proxyHeaderWhiteList` to share certain headers with the proxy.
504
505 You can also set the `proxyHeaderExclusiveList` to share certain
506 headers only with the proxy and not with destination host.
507
508 By default, this set is:
509
510 ```
511 accept
512 accept-charset
513 accept-encoding
514 accept-language
515 accept-ranges
516 cache-control
517 content-encoding
518 content-language
519 content-length
520 content-location
521 content-md5
522 content-range
523 content-type
524 connection
525 date
526 expect
527 max-forwards
528 pragma
529 proxy-authorization
530 referer
531 te
532 transfer-encoding
533 user-agent
534 via
535 ```
536
537 Note that, when using a tunneling proxy, the `proxy-authorization`
538 header and any headers from custom `proxyHeaderExclusiveList` are
539 *never* sent to the endpoint server, but only to the proxy server.
540
541
542 ### Controlling proxy behaviour using environment variables
543
544 The following environment variables are respected by `request`:
545
546 * `HTTP_PROXY` / `http_proxy`
547 * `HTTPS_PROXY` / `https_proxy`
548 * `NO_PROXY` / `no_proxy`
549
550 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.
551
552 `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.
553
554 Here's some examples of valid `no_proxy` values:
555
556 * `google.com` - don't proxy HTTP/HTTPS requests to Google.
557 * `google.com:443` - don't proxy HTTPS requests to Google, but *do* proxy HTTP requests to Google.
558 * `google.com:443, yahoo.com:80` - don't proxy HTTPS requests to Google, and don't proxy HTTP requests to Yahoo!
559 * `*` - ignore `https_proxy`/`http_proxy` environment variables altogether.
560
561 [back to top](#table-of-contents)
562
563
564 ---
565
566
567 ## UNIX Domain Sockets
568
569 `request` supports making requests to [UNIX Domain Sockets](http://en.wikipedia.org/wiki/Unix_domain_socket). To make one, use the following URL scheme:
570
571 ```js
572 /* Pattern */ 'http://unix:SOCKET:PATH'
573 /* Example */ request.get('http://unix:/absolute/path/to/unix.socket:/request/path')
8795fc6 @tcort README.md: add custom HTTP Headers example.
tcort authored
574 ```
668a291 @mikeal Adding some documentation for OAuth signing support.
mikeal authored
575
28e957c @simov Add table of contents in readme
simov authored
576 Note: The `SOCKET` path is assumed to be absolute to the root of the host file system.
577
578 [back to top](#table-of-contents)
579
580
581 ---
582
583
d53cf87 @shawnzhu doc for TLS/SSL protocol options
shawnzhu authored
584 ## TLS/SSL Protocol
585
586 TLS/SSL Protocol options, such as `cert`, `key` and `passphrase`, can be
41742f7 @mainakae Fixing documentation regarding TLS options (#1583)
mainakae authored
587 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).
588
589 ```js
590 var fs = require('fs')
591 , path = require('path')
592 , certFile = path.resolve(__dirname, 'ssl/client.crt')
593 , keyFile = path.resolve(__dirname, 'ssl/client.key')
594 , caFile = path.resolve(__dirname, 'ssl/ca.cert.pem')
595 , request = require('request');
596
597 var options = {
598 url: 'https://api.some-server.com/',
599 cert: fs.readFileSync(certFile),
600 key: fs.readFileSync(keyFile),
601 passphrase: 'password',
602 ca: fs.readFileSync(caFile)
603 }
604 };
605
606 request.get(options);
607 ```
608
609 ### Using `options.agentOptions`
610
d53cf87 @shawnzhu doc for TLS/SSL protocol options
shawnzhu authored
611 In the example below, we call an API requires client side SSL certificate
612 (in PEM format) with passphrase protected private key (in PEM format) and disable the SSLv3 protocol:
613
28e957c @simov Add table of contents in readme
simov authored
614 ```js
d53cf87 @shawnzhu doc for TLS/SSL protocol options
shawnzhu authored
615 var fs = require('fs')
616 , path = require('path')
617 , certFile = path.resolve(__dirname, 'ssl/client.crt')
618 , keyFile = path.resolve(__dirname, 'ssl/client.key')
619 , request = require('request');
620
621 var options = {
622 url: 'https://api.some-server.com/',
623 agentOptions: {
7842b17 @hypesystem Style update for TLS/SSL section in README (#1229)
hypesystem authored
624 cert: fs.readFileSync(certFile),
625 key: fs.readFileSync(keyFile),
d53cf87 @shawnzhu doc for TLS/SSL protocol options
shawnzhu authored
626 // Or use `pfx` property replacing `cert` and `key` when using private key, certificate and CA certs in PFX or PKCS12 format:
7842b17 @hypesystem Style update for TLS/SSL section in README (#1229)
hypesystem authored
627 // pfx: fs.readFileSync(pfxFilePath),
628 passphrase: 'password',
629 securityOptions: 'SSL_OP_NO_SSLv3'
d53cf87 @shawnzhu doc for TLS/SSL protocol options
shawnzhu authored
630 }
631 };
632
633 request.get(options);
634 ```
635
636 It is able to force using SSLv3 only by specifying `secureProtocol`:
637
28e957c @simov Add table of contents in readme
simov authored
638 ```js
d53cf87 @shawnzhu doc for TLS/SSL protocol options
shawnzhu authored
639 request.get({
640 url: 'https://api.some-server.com/',
641 agentOptions: {
7842b17 @hypesystem Style update for TLS/SSL section in README (#1229)
hypesystem authored
642 secureProtocol: 'SSLv3_method'
d53cf87 @shawnzhu doc for TLS/SSL protocol options
shawnzhu authored
643 }
512dcfc @hypesystem Document how to use custom CA in README (#1229)
hypesystem authored
644 });
645 ```
646
ea5ad4c @hypesystem Better explanation of use case for custom CAs (#1229)
hypesystem authored
647 It is possible to accept other certificates than those signed by generally allowed Certificate Authorities (CAs).
648 This can be useful, for example, when using self-signed certificates.
1727479 @jeffcharles Clarify the nature of setting `ca` in `agentOptions`
jeffcharles authored
649 To require a different root certificate, you can specify the signing CA by adding the contents of the CA's certificate file to the `agentOptions`.
650 The certificate the domain presents must be signed by the root certificate specified:
512dcfc @hypesystem Document how to use custom CA in README (#1229)
hypesystem authored
651
28e957c @simov Add table of contents in readme
simov authored
652 ```js
512dcfc @hypesystem Document how to use custom CA in README (#1229)
hypesystem authored
653 request.get({
654 url: 'https://api.some-server.com/',
655 agentOptions: {
7842b17 @hypesystem Style update for TLS/SSL section in README (#1229)
hypesystem authored
656 ca: fs.readFileSync('ca.cert.pem')
512dcfc @hypesystem Document how to use custom CA in README (#1229)
hypesystem authored
657 }
d53cf87 @shawnzhu doc for TLS/SSL protocol options
shawnzhu authored
658 });
659 ```
660
28e957c @simov Add table of contents in readme
simov authored
661 [back to top](#table-of-contents)
662
663
664 ---
665
d8ee939 @ahmadnassri fix typos + README section with feedback from @nylen
ahmadnassri authored
666 ## Support for HAR 1.2
667
668 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`.
669
670 a validation step will check if the HAR Request format matches the latest spec (v1.2) and will skip parsing if not matching.
671
672 ```js
673 var request = require('request')
674 request({
675 // will be ignored
676 method: 'GET'
677 uri: 'http://www.google.com',
678
679 // HTTP Archive Request Object
680 har: {
681 url: 'http://www.mockbin.com/har'
682 method: 'POST',
683 headers: [
684 {
685 name: 'content-type',
686 value: 'application/x-www-form-urlencoded'
687 }
688 ],
689 postData: {
690 mimeType: 'application/x-www-form-urlencoded',
691 params: [
692 {
693 name: 'foo',
694 value: 'bar'
695 },
696 {
697 name: 'hello',
698 value: 'world'
699 }
700 ]
701 }
702 }
703 })
704
705 // a POST request will be sent to http://www.mockbin.com
706 // with body an application/x-www-form-urlencoded body:
707 // foo=bar&hello=world
708 ```
709
710 [back to top](#table-of-contents)
711
712
713 ---
28e957c @simov Add table of contents in readme
simov authored
714
9ebcb87 @nylen Fix readme headings
nylen authored
715 ## request(options, callback)
758f598 @mikeal Initial commit. Request package.
mikeal authored
716
9072ff1 Edited README.md for formatting and clarity of phrasing
Tony authored
717 The first argument can be either a `url` or an `options` object. The only required option is `uri`; all others are optional.
12a4cb8 @mikeal Matching node documentation style.
mikeal authored
718
28e957c @simov Add table of contents in readme
simov authored
719 - `uri` || `url` - fully qualified uri or a parsed url object from `url.parse()`
720 - `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.
721 - `method` - http method (default: `"GET"`)
722 - `headers` - http headers (default: `{}`)
723
724 ---
725
726 - `qs` - object containing querystring values to be appended to the `uri`
5cdcbe5 @simov Add docs and tests for passing options to the querystring's
simov authored
727 - `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:{}}`
728 - `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`
28e957c @simov Add table of contents in readme
simov authored
729 - `useQuerystring` - If true, use `querystring` to stringify and parse
77d951b Add ability to specifiy querystring lib in options.
Joe Grund authored
730 querystrings, otherwise use `qs` (default: `false`). Set this option to
731 `true` if you need arrays to be serialized as `foo=bar&foo=baz` instead of the
732 default `foo[0]=bar&foo[1]=baz`.
28e957c @simov Add table of contents in readme
simov authored
733
734 ---
735
736 - `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.
737 - `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.
738 - `formData` - Data to pass for a `multipart/form-data` request. See
8c4d81f @nylen Tweak multipart docs
nylen authored
739 [Forms](#forms) section above.
28e957c @simov Add table of contents in readme
simov authored
740 - `multipart` - array of objects which contain their own headers and `body`
8c4d81f @nylen Tweak multipart docs
nylen authored
741 attributes. Sends a `multipart/related` request. See [Forms](#forms) section
742 above.
28e957c @simov Add table of contents in readme
simov authored
743 - Alternatively you can pass in an object `{chunked: false, data: []}` where
8c4d81f @nylen Tweak multipart docs
nylen authored
744 `chunked` is used to specify whether the request is sent in
745 [chunked transfer encoding](https://en.wikipedia.org/wiki/Chunked_transfer_encoding)
f48a577 @simov Fix multipart docs
simov authored
746 In non-chunked requests, data items with body streams are not allowed.
28e957c @simov Add table of contents in readme
simov authored
747 - `preambleCRLF` - append a newline/CRLF before the boundary of your `multipart/form-data` request.
748 - `postambleCRLF` - append a newline/CRLF at the end of the boundary of your `multipart/form-data` request.
749 - `json` - sets `body` but to JSON representation of value and adds `Content-type: application/json` header. Additionally, parses the response body as JSON.
750 - `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.
751
752 ---
753
754 - `auth` - A hash containing values `user` || `username`, `pass` || `password`, and `sendImmediately` (optional). See documentation above.
755 - `oauth` - Options for OAuth HMAC-SHA1 signing. See documentation above.
756 - `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).
757 - `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)
758 - `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.
759
760 ---
761
762 - `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.
763 - `followAllRedirects` - follow non-GET HTTP 3xx responses as redirects (default: `false`)
764 - `maxRedirects` - the maximum number of redirects to follow (default: `10`)
3249d71 @simov A few minor fixes:
simov authored
765 - `removeRefererHeader` - removes the referer header when a redirect happens (default: `false`).
28e957c @simov Add table of contents in readme
simov authored
766
767 ---
768
9978a48 @daniel347x Update README.md
daniel347x authored
769 - `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). (**Note:** if you expect binary data, you should set `encoding: null`.)
28e957c @simov Add table of contents in readme
simov authored
770 - `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.
771 - `jar` - If `true` and `tough-cookie` is installed, remember cookies for future use (or define your custom cookie jar; see examples section)
772
773 ---
774
5cdfe8e @simov Add tests and docs for using the agent, agentClass and agentOptions
simov authored
775 - `agent` - `http(s).Agent` instance to use
776 - `agentClass` - alternatively specify your agent's class name
777 - `agentOptions` - and pass its options. **Note:** for HTTPS see [tls API doc for TLS/SSL options](http://nodejs.org/api/tls.html#tls_tls_connect_options_callback) and the [documentation above](#using-optionsagentoptions).
391b80b @simov Remove forever option warning and improve docs
simov authored
778 - `forever` - set to `true` to use the [forever-agent](https://github.com/request/forever-agent) **Note:** Defaults to `http(s).Agent({keepAlive:true})` in node 0.12+
5cdfe8e @simov Add tests and docs for using the agent, agentClass and agentOptions
simov authored
779 - `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). 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. **Note:** `pool` is used only when the `agent` option is not specified.
28e957c @simov Add table of contents in readme
simov authored
780 - 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}`).
781 - Note that if you are sending multiple requests in a loop and creating
0d22579 @nylen Improve documentation for pool and maxSockets options
nylen authored
782 multiple new `pool` objects, `maxSockets` will not work as intended. To
783 work around this, either use [`request.defaults`](#requestdefaultsoptions)
784 with your pool options or create the pool object with the `maxSockets`
785 property outside of the loop.
28e957c @simov Add table of contents in readme
simov authored
786 - `timeout` - Integer containing the number of milliseconds to wait for a
772e141 @watson Improve `timeout` option description
watson authored
787 request to respond before aborting the request. Note that if the underlying
788 TCP connection cannot be established, the OS-wide TCP connection timeout will
789 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)).
5cdfe8e @simov Add tests and docs for using the agent, agentClass and agentOptions
simov authored
790
791 ---
792
28e957c @simov Add table of contents in readme
simov authored
793 - `localAddress` - Local interface to bind for network connections.
794 - `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`)
795 - `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.
796 - `tunnel` - controls the behavior of
0da471f @nylen Allow explicitly disabling tunneling for proxied https destinations
nylen authored
797 [HTTP `CONNECT` tunneling](https://en.wikipedia.org/wiki/HTTP_tunnel#HTTP_CONNECT_tunneling)
798 as follows:
28e957c @simov Add table of contents in readme
simov authored
799 - `undefined` (default) - `true` if the destination is `https` or a previous
0da471f @nylen Allow explicitly disabling tunneling for proxied https destinations
nylen authored
800 request in the redirect chain used a tunneling proxy, `false` otherwise
28e957c @simov Add table of contents in readme
simov authored
801 - `true` - always tunnel to the destination by making a `CONNECT` request to
0da471f @nylen Allow explicitly disabling tunneling for proxied https destinations
nylen authored
802 the proxy
28e957c @simov Add table of contents in readme
simov authored
803 - `false` - request the destination as a `GET` request.
804 - `proxyHeaderWhiteList` - A whitelist of headers to send to a
9a84058 @isaacs Document tunnel and proxyHeaderWhiteList options
isaacs authored
805 tunneling proxy.
28e957c @simov Add table of contents in readme
simov authored
806 - `proxyHeaderExclusiveList` - A whitelist of headers to send
7d5e2c2 @RReverser Renamed new option to proxyHeaderExclusiveList for clarity.
RReverser authored
807 exclusively to a tunneling proxy and not to destination.
28e957c @simov Add table of contents in readme
simov authored
808
809 ---
810
811 - `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.
d8ee939 @ahmadnassri fix typos + README section with feedback from @nylen
ahmadnassri authored
812 - `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)*
12a4cb8 @mikeal Matching node documentation style.
mikeal authored
813
669200a @dotcypress add metadata for form-data file field
dotcypress authored
814 The callback argument gets 3 arguments:
9072ff1 Edited README.md for formatting and clarity of phrasing
Tony authored
815
fef5bf3 @mmalecki Fix callback arguments documentation
mmalecki authored
816 1. An `error` when applicable (usually from [`http.ClientRequest`](http://nodejs.org/api/http.html#http_class_http_clientrequest) object)
817 2. An [`http.IncomingMessage`](http://nodejs.org/api/http.html#http_http_incomingmessage) object
9d73e5a @iansltx add note about JSON output body type
iansltx authored
818 3. The third is the `response` body (`String` or `Buffer`, or JSON object if the `json` option is supplied)
12a4cb8 @mikeal Matching node documentation style.
mikeal authored
819
28e957c @simov Add table of contents in readme
simov authored
820 [back to top](#table-of-contents)
821
822
823 ---
824
7b2f788 @mikeal More doc cleanup.
mikeal authored
825 ## Convenience methods
826
827 There are also shorthand methods for different HTTP METHODs and some other conveniences.
828
28e957c @simov Add table of contents in readme
simov authored
829
f98ff99 @mikeal Update this old ass readme with some new HOTNESS!
mikeal authored
830 ### request.defaults(options)
831
7afda90 @nylen Clarify that defaults() does not modify global defaults
nylen authored
832 This method **returns a wrapper** around the normal request API that defaults
833 to whatever options you pass to it.
7b2f788 @mikeal More doc cleanup.
mikeal authored
834
7afda90 @nylen Clarify that defaults() does not modify global defaults
nylen authored
835 **Note:** `request.defaults()` **does not** modify the global request API;
836 instead, it **returns a wrapper** that has your default settings applied to it.
837
838 **Note:** You can call `.defaults()` on the wrapper that is returned from
839 `request.defaults` to add/override defaults that were previously defaulted.
aa5395e @tikotzky docs for recursive request.defaults
tikotzky authored
840
841 For example:
28e957c @simov Add table of contents in readme
simov authored
842 ```js
aa5395e @tikotzky docs for recursive request.defaults
tikotzky authored
843 //requests using baseRequest() will set the 'x-token' header
844 var baseRequest = request.defaults({
845 headers: {x-token: 'my-token'}
846 })
847
848 //requests using specialRequest() will include the 'x-token' header set in
849 //baseRequest and will also include the 'special' header
850 var specialRequest = baseRequest.defaults({
851 headers: {special: 'special value'}
852 })
853 ```
854
7b2f788 @mikeal More doc cleanup.
mikeal authored
855 ### request.put
856
9072ff1 Edited README.md for formatting and clarity of phrasing
Tony authored
857 Same as `request()`, but defaults to `method: "PUT"`.
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
858
28e957c @simov Add table of contents in readme
simov authored
859 ```js
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
860 request.put(url)
7b2f788 @mikeal More doc cleanup.
mikeal authored
861 ```
862
aa4a285 @mloar Add patch convenience method.
mloar authored
863 ### request.patch
864
9072ff1 Edited README.md for formatting and clarity of phrasing
Tony authored
865 Same as `request()`, but defaults to `method: "PATCH"`.
aa4a285 @mloar Add patch convenience method.
mloar authored
866
28e957c @simov Add table of contents in readme
simov authored
867 ```js
aa4a285 @mloar Add patch convenience method.
mloar authored
868 request.patch(url)
869 ```
870
7b2f788 @mikeal More doc cleanup.
mikeal authored
871 ### request.post
872
9072ff1 Edited README.md for formatting and clarity of phrasing
Tony authored
873 Same as `request()`, but defaults to `method: "POST"`.
7b2f788 @mikeal More doc cleanup.
mikeal authored
874
28e957c @simov Add table of contents in readme
simov authored
875 ```js
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
876 request.post(url)
7b2f788 @mikeal More doc cleanup.
mikeal authored
877 ```
878
879 ### request.head
880
395d244 Update README.md
Aaron Hartwig authored
881 Same as `request()`, but defaults to `method: "HEAD"`.
7b2f788 @mikeal More doc cleanup.
mikeal authored
882
28e957c @simov Add table of contents in readme
simov authored
883 ```js
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
884 request.head(url)
7b2f788 @mikeal More doc cleanup.
mikeal authored
885 ```
886
887 ### request.del
888
9072ff1 Edited README.md for formatting and clarity of phrasing
Tony authored
889 Same as `request()`, but defaults to `method: "DELETE"`.
7b2f788 @mikeal More doc cleanup.
mikeal authored
890
28e957c @simov Add table of contents in readme
simov authored
891 ```js
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
892 request.del(url)
893 ```
894
7b2f788 @mikeal More doc cleanup.
mikeal authored
895 ### request.get
896
9072ff1 Edited README.md for formatting and clarity of phrasing
Tony authored
897 Same as `request()` (for uniformity).
7b2f788 @mikeal More doc cleanup.
mikeal authored
898
28e957c @simov Add table of contents in readme
simov authored
899 ```js
7b2f788 @mikeal More doc cleanup.
mikeal authored
900 request.get(url)
901 ```
6181b7a Documented request.cookie() and request.jar()
alessioalex authored
902 ### request.cookie
903
904 Function that creates a new cookie.
905
28e957c @simov Add table of contents in readme
simov authored
906 ```js
1988226 @seanstrom update the request.cookie docs to have a valid cookie example
seanstrom authored
907 request.cookie('key1=value1')
6181b7a Documented request.cookie() and request.jar()
alessioalex authored
908 ```
018362f @nylen Improve documentation of custom cookie store
nylen authored
909 ### request.jar()
6181b7a Documented request.cookie() and request.jar()
alessioalex authored
910
911 Function that creates a new cookie jar.
912
28e957c @simov Add table of contents in readme
simov authored
913 ```js
6181b7a Documented request.cookie() and request.jar()
alessioalex authored
914 request.jar()
915 ```
7b2f788 @mikeal More doc cleanup.
mikeal authored
916
28e957c @simov Add table of contents in readme
simov authored
917 [back to top](#table-of-contents)
918
919
920 ---
921
922
923 ## Debugging
924
925 There are at least three ways to debug the operation of `request`:
926
927 1. Launch the node process like `NODE_DEBUG=request node script.js`
928 (`lib,request,otherlib` works too).
929
930 2. Set `require('request').debug = true` at any time (this does the same thing
931 as #1).
932
933 3. Use the [request-debug module](https://github.com/nylen/request-debug) to
934 view request and response headers and bodies.
935
936 [back to top](#table-of-contents)
937
938
939 ---
940
7b2f788 @mikeal More doc cleanup.
mikeal authored
941
942 ## Examples:
4ff3493 @mikeal Updates to README.md
mikeal authored
943
28e957c @simov Add table of contents in readme
simov authored
944 ```js
e477369 @mikeal Doc refinements.
mikeal authored
945 var request = require('request')
946 , rand = Math.floor(Math.random()*100000000).toString()
947 ;
ab4c96b @mikeal Moved spec tests to normal node script tests. Style changes to code a…
mikeal authored
948 request(
949 { method: 'PUT'
4e4d428 @jhs Update to Iris Couch URL
jhs authored
950 , uri: 'http://mikeal.iriscouch.com/testjs/' + rand
f98ff99 @mikeal Update this old ass readme with some new HOTNESS!
mikeal authored
951 , multipart:
ab4c96b @mikeal Moved spec tests to normal node script tests. Style changes to code a…
mikeal authored
952 [ { 'content-type': 'application/json'
953 , body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
954 }
955 , { body: 'I am an attachment' }
f98ff99 @mikeal Update this old ass readme with some new HOTNESS!
mikeal authored
956 ]
af66607 @Stanley Made specs pass
Stanley authored
957 }
ab4c96b @mikeal Moved spec tests to normal node script tests. Style changes to code a…
mikeal authored
958 , function (error, response, body) {
959 if(response.statusCode == 201){
4e4d428 @jhs Update to Iris Couch URL
jhs authored
960 console.log('document saved as: http://mikeal.iriscouch.com/testjs/'+ rand)
ab4c96b @mikeal Moved spec tests to normal node script tests. Style changes to code a…
mikeal authored
961 } else {
e477369 @mikeal Doc refinements.
mikeal authored
962 console.log('error: '+ response.statusCode)
963 console.log(body)
ab4c96b @mikeal Moved spec tests to normal node script tests. Style changes to code a…
mikeal authored
964 }
965 }
966 )
47314d7 @mikeal Greatly expanded documentation.
mikeal authored
967 ```
9072ff1 Edited README.md for formatting and clarity of phrasing
Tony authored
968
9baf5ac @kevinoid [PATCH v4] Clarify gzip handling in the README
kevinoid authored
969 For backwards-compatibility, response compression is not supported by default.
970 To accept gzip-compressed responses, set the `gzip` option to `true`. Note
971 that the body data passed through `request` is automatically decompressed
972 while the response object is unmodified and will contain compressed data if
973 the server sent a compressed response.
974
28e957c @simov Add table of contents in readme
simov authored
975 ```js
9baf5ac @kevinoid [PATCH v4] Clarify gzip handling in the README
kevinoid authored
976 var request = require('request')
977 request(
978 { method: 'GET'
979 , uri: 'http://www.google.com'
980 , gzip: true
981 }
982 , function (error, response, body) {
983 // body is the decompressed response body
984 console.log('server encoded the data as: ' + (response.headers['content-encoding'] || 'identity'))
985 console.log('the decoded data is: ' + body)
986 }
987 ).on('data', function(data) {
988 // decompressed data as it is received
989 console.log('decoded chunk: ' + data)
990 })
991 .on('response', function(response) {
992 // unmodified http.IncomingMessage object
993 response.on('data', function(data) {
994 // compressed data as it is received
995 console.log('received ' + data.length + ' bytes of compressed data')
996 })
997 })
998 ```
999
6d10557 @wbyoung Documented tough-cookie installation.
wbyoung authored
1000 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`.
b519044 request.defaults({jar: false}) disables cookies && also updated README
alessioalex authored
1001
28e957c @simov Add table of contents in readme
simov authored
1002 ```js
e77746b @threepointone global cookie jar disabled by default, send jar: true to enable.
threepointone authored
1003 var request = request.defaults({jar: true})
b519044 request.defaults({jar: false}) disables cookies && also updated README
alessioalex authored
1004 request('http://www.google.com', function () {
1005 request('http://images.google.com')
1006 })
1007 ```
1008
b8ee579 @dandv Fix typo
dandv authored
1009 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`)
8323eed Closed issue 82 : handling cookies - added tests too
alessioalex authored
1010
28e957c @simov Add table of contents in readme
simov authored
1011 ```js
8323eed Closed issue 82 : handling cookies - added tests too
alessioalex authored
1012 var j = request.jar()
1013 var request = request.defaults({jar:j})
1014 request('http://www.google.com', function () {
1015 request('http://images.google.com')
1016 })
1017 ```
3db7f7d Updated README
alessioalex authored
1018
cbee3d0 @stash Use tough-cookie CookieJar sync API
stash authored
1019 OR
4d09556 @emkay updating setCookie example to make it clear that the callback is requ…
emkay authored
1020
28e957c @simov Add table of contents in readme
simov authored
1021 ```js
1af58d3 @pipi32167 Fix cookie example in README.md and make it more clear
pipi32167 authored
1022 var j = request.jar();
1023 var cookie = request.cookie('key1=value1');
1024 var url = 'http://www.google.com';
a5f8f26 @FredKSchott fix README outdated reference to setCookieSync old name
FredKSchott authored
1025 j.setCookie(cookie, url);
1af58d3 @pipi32167 Fix cookie example in README.md and make it more clear
pipi32167 authored
1026 request({url: url, jar: j}, function () {
8323eed Closed issue 82 : handling cookies - added tests too
alessioalex authored
1027 request('http://images.google.com')
1028 })
1029 ```
adcf62b @charlespwd update readme
charlespwd authored
1030
018362f @nylen Improve documentation of custom cookie store
nylen authored
1031 To use a custom cookie store (such as a
1032 [`FileCookieStore`](https://github.com/mitsuru/tough-cookie-filestore)
1033 which supports saving to and restoring from JSON files), pass it as a parameter
1034 to `request.jar()`:
1d4b420 @mitsuru Update README for custom cookie store
mitsuru authored
1035
28e957c @simov Add table of contents in readme
simov authored
1036 ```js
1d4b420 @mitsuru Update README for custom cookie store
mitsuru authored
1037 var FileCookieStore = require('tough-cookie-filestore');
018362f @nylen Improve documentation of custom cookie store
nylen authored
1038 // NOTE - currently the 'cookies.json' file must already exist!
1039 var j = request.jar(new FileCookieStore('cookies.json'));
1040 request = request.defaults({ jar : j })
1d4b420 @mitsuru Update README for custom cookie store
mitsuru authored
1041 request('http://www.google.com', function() {
1042 request('http://images.google.com')
1043 })
1044 ```
1045
018362f @nylen Improve documentation of custom cookie store
nylen authored
1046 The cookie store must be a
1047 [`tough-cookie`](https://github.com/goinstant/tough-cookie)
1048 store and it must support synchronous operations; see the
1049 [`CookieStore` API docs](https://github.com/goinstant/tough-cookie/#cookiestore-api)
1050 for details.
1051
1052 To inspect your cookie jar after a request:
adcf62b @charlespwd update readme
charlespwd authored
1053
28e957c @simov Add table of contents in readme
simov authored
1054 ```js
669200a @dotcypress add metadata for form-data file field
dotcypress authored
1055 var j = request.jar()
adcf62b @charlespwd update readme
charlespwd authored
1056 request({url: 'http://www.google.com', jar: j}, function () {
47a9734 @nylen Revert "Explicitly use sync versions of cookie functions"
nylen authored
1057 var cookie_string = j.getCookieString(uri); // "key1=value1; key2=value2; ..."
669200a @dotcypress add metadata for form-data file field
dotcypress authored
1058 var cookies = j.getCookies(uri);
adcf62b @charlespwd update readme
charlespwd authored
1059 // [{key: 'key1', value: 'value1', domain: "www.google.com", ...}, ...]
1060 })
1061 ```
c7648a4 @nylen Document debug options
nylen authored
1062
28e957c @simov Add table of contents in readme
simov authored
1063 [back to top](#table-of-contents)
Something went wrong with that request. Please try again.