Skip to content
Browse files

Allow request's body to be an object.

  • Loading branch information...
1 parent 3153436 commit 875f79b6a40340457fafafdadac813cfa5343689 @Stanley Stanley committed
Showing with 17 additions and 4 deletions.
  1. +2 −2 README.md
  2. +7 −1 main.js
  3. +8 −1 tests/couch.js
View
4 README.md
@@ -17,7 +17,7 @@ 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
+* `body` - entity body for POST and PUT requests. Must be buffer, string or object.
* `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.
@@ -38,4 +38,4 @@ Example:
})
</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.
+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.
View
8 main.js
@@ -101,7 +101,13 @@ function request (options, callback) {
if (options.proxy) options.fullpath = (options.uri.protocol + '//' + options.uri.host + options.fullpath);
if (options.body !== undefined) {
- options.body = Buffer.isBuffer(options.body) ? options.body : new Buffer(options.body);
+ if(!Buffer.isBuffer(options.body)){
+ if(typeof options.body == 'object'){
+ options.body = JSON.stringify(options.body);
+ options.headers['content-type'] = 'application/json';
+ }
+ options.body = new Buffer(options.body);
+ }
options.headers['content-length'] = options.body.length;
}
options.request = options.client.request(options.method, options.fullpath, options.headers);
View
9 tests/couch.js
@@ -36,10 +36,17 @@ request({uri:'http://mikeal.couchone.com/testjs', method:'POST', headers: h, bod
assert.equal(response.statusCode, 201, body);
});
+var randomnumber=Math.floor(Math.random()*100000000).toString();
+request({uri:'http://mikeal.couchone.com/testjs', method:'POST', body:{"_id": 'randomnumber'}},
+ function (error, response, body) {
+ if (error) {throw new Error(error)};
+ assert.equal(response.statusCode, 201, body);
+ });
+
var options = {uri:'http://gmail.com'};
request(options, function (error, response, body) {
if (error) throw error;
assert.equal(response.statusCode, 200);
assert.equal(options.uri.host, 'www.google.com');
assert.equal(response.socket.port, 443);
-})
+})

0 comments on commit 875f79b

Please sign in to comment.
Something went wrong with that request. Please try again.