Comparing version 0.5.9 to 0.6.0
@@ -41,3 +41,3 @@ ////////////////////////////////////////// | ||
var parsers = { | ||
'application/json': function(data, callback){ | ||
'application/json': function(data, callback) { | ||
try { | ||
@@ -53,5 +53,5 @@ callback(null, data && JSON.parse(data)); | ||
var xml2js = require('xml2js'); | ||
parsers['application/xml'] = function(data, callback){ | ||
parsers['application/xml'] = function(data, callback) { | ||
var xml_parser = new xml2js.Parser({ explicitRoot: true, explicitArray: false }); | ||
xml_parser.parseString(data, function(err, result){ | ||
xml_parser.parseString(data, function(err, result) { | ||
callback(err, err ? data : result); // return original if err failed | ||
@@ -86,3 +86,3 @@ }); | ||
request: function(method, uri, data, options, callback){ | ||
request: function(method, uri, data, options, callback) { | ||
@@ -108,3 +108,3 @@ var self = this; | ||
// if any of node's TLS options are passed, let them be passed to https.request() | ||
node_tls_opts.split(' ').forEach(function(key){ | ||
node_tls_opts.split(' ').forEach(function(key) { | ||
if (typeof options[key] != 'undefined') { | ||
@@ -141,3 +141,3 @@ config.base_opts[key] = options[key]; | ||
var boundary = options.boundary || defaults.boundary; | ||
return multipart.build(data, boundary, function(err, body){ | ||
return multipart.build(data, boundary, function(err, body) { | ||
if (err) throw(err); | ||
@@ -167,3 +167,3 @@ config.headers['Content-Type'] = 'multipart/form-data; boundary=' + boundary; | ||
get_request_opts: function(method, uri, config){ | ||
get_request_opts: function(method, uri, config) { | ||
var opts = config.base_opts, proxy = config.proxy; | ||
@@ -195,3 +195,3 @@ var remote = proxy ? url.parse(proxy) : url.parse(uri); | ||
send_request: function(count, method, uri, config, post_data, callback){ | ||
send_request: function(count, method, uri, config, post_data, callback) { | ||
@@ -205,7 +205,7 @@ var timer, | ||
config.out = new Readable(); | ||
config.out.on('error', function(er){ /* noop */ }) | ||
config.out.on('error', function(er) { /* noop */ }) | ||
} | ||
debug('Making request #' + count, request_opts); | ||
var request = protocol.request(request_opts, function(resp){ | ||
var request = protocol.request(request_opts, function(resp) { | ||
@@ -253,3 +253,3 @@ var headers = resp.headers; | ||
resp.on('data', function(chunk){ | ||
resp.on('data', function(chunk) { | ||
chunks.push(chunk); | ||
@@ -261,3 +261,3 @@ if (config.out) config.out.push(chunk); | ||
resp.on('end', function(){ | ||
resp.on('end', function() { | ||
if (config.out) config.out.push(null); // close readable out stream | ||
@@ -270,3 +270,3 @@ | ||
for (var i = 0, len = chunks.length; i < len; i++) { | ||
chunks[i].copy(resp.body, pos); | ||
(new Buffer(chunks[i])).copy(resp.body, pos); | ||
pos += chunks[i].length; | ||
@@ -279,3 +279,3 @@ } | ||
unzip(resp.body, function(err, buff){ | ||
unzip(resp.body, function(err, buff) { | ||
if (!err && buff) resp.body = buff; // only if no errors | ||
@@ -308,3 +308,3 @@ self.response_end(response_opts, resp, callback); | ||
parse_content_type: function(header){ | ||
parse_content_type: function(header) { | ||
if (!header || header == '') return {}; | ||
@@ -318,6 +318,6 @@ | ||
response_end: function(opts, resp, callback){ | ||
response_end: function(opts, resp, callback) { | ||
if (!callback) return; // no point in going ahead | ||
var handle_output = function(err, final){ | ||
var handle_output = function(err, final) { | ||
if (final) resp.body = final; | ||
@@ -329,3 +329,3 @@ | ||
// err is nil and output requested, so write to file | ||
fs.writeFile(opts.output, resp.body, function(e){ | ||
fs.writeFile(opts.output, resp.body, function(e) { | ||
callback(e, resp, resp.body); | ||
@@ -360,19 +360,19 @@ }) | ||
exports.head = function(uri, options, callback){ | ||
exports.head = function(uri, options, callback) { | ||
return Needle.request('HEAD', uri, null, options, callback); | ||
} | ||
exports.get = function(uri, options, callback){ | ||
exports.get = function(uri, options, callback) { | ||
return Needle.request('GET', uri, null, options, callback); | ||
} | ||
exports.post = function(uri, data, options, callback){ | ||
exports.post = function(uri, data, options, callback) { | ||
return Needle.request('POST', uri, data, options, callback); | ||
} | ||
exports.put = function(uri, data, options, callback){ | ||
exports.put = function(uri, data, options, callback) { | ||
return Needle.request('PUT', uri, data, options, callback); | ||
} | ||
exports.delete = function(uri, data, options, callback){ | ||
exports.delete = function(uri, data, options, callback) { | ||
return Needle.request('DELETE', uri, data, options, callback); | ||
@@ -379,0 +379,0 @@ } |
{ | ||
"name": "needle" | ||
, "version": "0.5.9" | ||
, "version": "0.6.0" | ||
, "description": "Tiny yet feature-packed HTTP client. With multipart, charset decoding and proxy support." | ||
@@ -5,0 +5,0 @@ , "keywords": ["http", "https", "simple", "request", "client", "multipart", "upload", "proxy", "deflate", "timeout", "charset", "iconv"] |
Needle | ||
====== | ||
The leanest and most handsome HTTP client in the Nodelands. Supports SSL, basic & digest auth, proxied requests, multipart form-data (e.g. file uploads), gzip/deflate compression, automatic XML/JSON parsing, follows redirects and decodes non-UTF-8 content. Two dependencies only. | ||
The leanest and most handsome HTTP client in the Nodelands. With only two dependencies, it supports: | ||
- HTTP and HTTPS requests | ||
- Basic & Digest auth | ||
- Forwarding via proxy | ||
- Multipart form-data (e.g. file uploads) | ||
- Gzip/deflate compression | ||
- Automatic XML & JSON parsing | ||
- 301/302 redirect following | ||
- Decodes non-UTF-8 content. | ||
Ideal for performing simple, quick HTTP requests in Node.js. If you need OAuth, AWS support or anything fancier, you should check out mikeal's request module. | ||
@@ -35,2 +44,5 @@ | ||
needle.delete(url, data, [options], callback); | ||
// and a generic one | ||
needle.request(method, url, data, [options], callback); | ||
``` | ||
@@ -56,2 +68,3 @@ Callback receives `(error, response, body)`. Needle returns the response stream, which means you can pipe it to your heart's content. | ||
- `auth` : Determines what to do with provided username/password. Options are `auto`, `digest` or `basic` (default). | ||
- `json` : When `true`, sets content type to `application/json` and sends request body as JSON string, instead of a query string. | ||
@@ -91,2 +104,3 @@ Response options | ||
- `rejectUnauthorized`: If true, the server certificate is verified against the list of supplied CAs. An 'error' event is emitted if verification fails. Verification happens at the connection level, before the HTTP request is sent. | ||
- `secureProtocol`: The SSL method to use, e.g. SSLv3_method to force SSL version 3. | ||
@@ -93,0 +107,0 @@ Examples |
@@ -118,3 +118,3 @@ var needle = require('../'), | ||
var timediff = (new Date() - time); | ||
timediff.should.be.within(200, 250); | ||
timediff.should.be.within(200, 300); | ||
done(); | ||
@@ -121,0 +121,0 @@ }) |
36622
257