Skip to content
This repository
Newer
Older
100644 79 lines (64 sloc) 3.331 kb
758f598d »
2010-05-07 Initial commit. Request package.
1 # Request -- Simplified HTTP request method
2
12a4cb88 »
2010-05-07 Matching node documentation style.
3 ## Install
758f598d »
2010-05-07 Initial commit. Request package.
4
5 <pre>
6 npm install request
7 </pre>
8
02f6b38c »
2011-01-30 Added specs for body option
9 Or from source:
10
11 <pre>
12 git clone git://github.com/mikeal/request.git
13 cd request
14 npm link .
15 </pre>
16
17 Running specs:
18 <pre>
19 node specs.js
20 </pre>
21 Note: `jasmine-node` package is required to run tests.
22
12a4cb88 »
2010-05-07 Matching node documentation style.
23 ## Super simple to use
758f598d »
2010-05-07 Initial commit. Request package.
24
4ff34937 »
2010-05-19 Updates to README.md
25 Request is designed to be the simplest way possible to make http calls. It support HTTPS and follows redirects by default.
26
12a4cb88 »
2010-05-07 Matching node documentation style.
27 #### request(options, callback)
758f598d »
2010-05-07 Initial commit. Request package.
28
12a4cb88 »
2010-05-07 Matching node documentation style.
29 The first argument is an options object. The only required option is uri, all others are optional.
30
fa72fcb9 »
2011-01-22 Updated documentation.
31 * `uri` || `url` - fully qualified uri or a parsed url object from url.parse()
32 * `method` - http method, defaults to GET
33 * `headers` - http headers, defaults to {}
af666072 »
2011-01-30 Made specs pass
34 * `body` - entity body for POST and PUT requests. Must be buffer or string.
35 * `json` - similar to `body` but converts value to string and adds `Content-type: application/json` header.
36 * `multipart` - (experimental) array of objects which contains their own headers and `body` attribute. Sends `multipart/related` request. See example below.
fa72fcb9 »
2011-01-22 Updated documentation.
37 * `client` - existing http client object (when undefined a new one will be created and assigned to this property so you can keep around a reference to it if you would like use keep-alive on later request)
38 * `followRedirect` - follow HTTP 3xx responses as redirects. defaults to true.
39 * `maxRedirects` - the maximum number of redirects to follow, defaults to 10.
40 * `onResponse` - If true the callback will be fired on the "response" event instead of "end". If a function it will be called on "response" and not effect the regular semantics of the main callback on "end".
41 * `encoding` - Encoding to be used on response.setEncoding when buffering the response data.
42 * `requestBodyStream` - Stream to read request body chunks from.
43 * `responseBodyStream` - Stream to write body chunks to. When set this option will be passed as the last argument to the callback instead of the entire body.
12a4cb88 »
2010-05-07 Matching node documentation style.
44
45 The callback argument gets 3 arguments. The first is an error when applicable (usually from the http.Client option not the http.ClientRequest object). The second in an http.ClientResponse object. The third is the response body buffer.
46
af666072 »
2011-01-30 Made specs pass
47 Examples:
104cc948 »
2010-05-07 Removing unnecessary markup.
48 <pre>
758f598d »
2010-05-07 Initial commit. Request package.
49 var request = require('request');
50 request({uri:'http://www.google.com'}, function (error, response, body) {
51 if (!error && response.statusCode == 200) {
52 sys.puts(body) // Print the google web page.
53 }
54 })
4ff34937 »
2010-05-19 Updates to README.md
55 </pre>
56
af666072 »
2011-01-30 Made specs pass
57 <pre>
58 var request = require('request');
59 var rand = Math.floor(Math.random()*100000000).toString();
60 request({
61 method: 'PUT',
62 uri: 'http://mikeal.couchone.com/testjs/'+ rand,
63 multipart: [
64 { 'content-type': 'application/json',
65 body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})},
66 { body: 'I am an attachment' }
67 ]
68 }, function (error, response, body) {
69 if(response.statusCode == 201){
70 console.log('document saved as: http://mikeal.couchone.com/testjs/'+ rand);
71 } else {
72 console.log('error: '+ response.statusCode);
73 console.log(body)
74 }
75 })
76 </pre>
77
875f79b6 »
2011-01-29 Allow request's body to be an object.
78 It's also worth noting that the options argument will mutate. When following a redirect the uri values will change. After setting up a client options it will set the client property.
Something went wrong with that request. Please try again.