Skip to content

Commit

Permalink
Made specs pass
Browse files Browse the repository at this point in the history
Added explanation and examples to README
  • Loading branch information
Stanley committed Jan 30, 2011
1 parent 02f6b38 commit af66607
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ The first argument is an options object. The only required option is uri, all ot
* `uri` || `url` - fully qualified uri or a parsed url object from url.parse()
* `method` - http method, defaults to GET
* `headers` - http headers, defaults to {}
* `body` - entity body for POST and PUT requests. Must be buffer, string or object.
* `body` - entity body for POST and PUT requests. Must be buffer or string.
* `json` - similar to `body` but converts value to string and adds `Content-type: application/json` header.
* `multipart` - (experimental) array of objects which contains their own headers and `body` attribute. Sends `multipart/related` request. See example below.
* `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)
* `followRedirect` - follow HTTP 3xx responses as redirects. defaults to true.
* `maxRedirects` - the maximum number of redirects to follow, defaults to 10.
Expand All @@ -42,7 +44,7 @@ The first argument is an options object. The only required option is uri, all ot

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.

Example:
Examples:
<pre>
var request = require('request');
request({uri:'http://www.google.com'}, function (error, response, body) {
Expand All @@ -52,4 +54,25 @@ Example:
})
</pre>

<pre>
var request = require('request');
var rand = Math.floor(Math.random()*100000000).toString();
request({
method: 'PUT',
uri: 'http://mikeal.couchone.com/testjs/'+ rand,
multipart: [
{ 'content-type': 'application/json',
body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})},
{ body: 'I am an attachment' }
]
}, function (error, response, body) {
if(response.statusCode == 201){
console.log('document saved as: http://mikeal.couchone.com/testjs/'+ rand);
} else {
console.log('error: '+ response.statusCode);
console.log(body)
}
})
</pre>

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.
35 changes: 28 additions & 7 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,37 @@ function request (options, callback) {

if (options.proxy) options.fullpath = (options.uri.protocol + '//' + options.uri.host + options.fullpath);

if (options.body !== undefined) {
if(!Buffer.isBuffer(options.body)){
if(typeof options.body == 'object'){
options.body = JSON.stringify(options.body);
options.headers['content-type'] = 'application/json';
}
if(options.json){
options.headers['content-type'] = 'application/json';
options.body = JSON.stringify(options.json);
} else if(options.multipart){
options.body = '';
options.headers['content-type'] = 'multipart/related;boundary="frontier"';

if(!options.multipart.forEach) throw new Error('Argument error');
options.multipart.forEach(function(part){
var body = part.body;
if(!body) throw Error('Body attribute missing')
delete part.body;
options.body += '--frontier\r\n';
Object.keys(part).forEach(function(key){
options.body += key + ': ' + part[key] + '\r\n'
})
options.body += '\r\n' + body + '\r\n';
})
options.body += '--frontier--'
}

if (options.body) {
if (!Buffer.isBuffer(options.body)) {
options.body = new Buffer(options.body);
}
options.headers['content-length'] = options.body.length;
if(options.body.length)
options.headers['content-length'] = options.body.length;
else
throw new Error('Argument error')
}

options.request = options.client.request(options.method, options.fullpath, options.headers);
options.request.addListener("response", function (response) {
if (setHost) delete options.headers.host;
Expand Down

0 comments on commit af66607

Please sign in to comment.