simple-get
Advanced tools
Comparing version 2.1.0 to 2.2.0
13
index.js
@@ -16,5 +16,6 @@ module.exports = simpleGet | ||
if (opts.headers == null) opts.headers = {} | ||
if (opts.json) opts.headers['content-type'] = 'application/json' | ||
if (opts.maxRedirects == null) opts.maxRedirects = 10 | ||
var body = opts.body | ||
var body = opts.json ? JSON.stringify(opts.body) : opts.body | ||
opts.body = undefined | ||
@@ -60,3 +61,11 @@ if (body && !opts.method) opts.method = 'POST' | ||
res.on('end', function () { | ||
cb(null, res, Buffer.concat(chunks)) | ||
var data = Buffer.concat(chunks) | ||
if (opts.json) { | ||
try { | ||
data = JSON.parse(data.toString()) | ||
} catch (err) { | ||
return cb(err, res, data) | ||
} | ||
} | ||
cb(null, res, data) | ||
}) | ||
@@ -63,0 +72,0 @@ }) |
{ | ||
"name": "simple-get", | ||
"description": "Simplest way to make http get requests. Supports HTTPS, redirects, gzip/deflate, streams in < 100 lines.", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Feross Aboukhadijeh", |
@@ -110,4 +110,25 @@ # simple-get [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] | ||
### JSON Handling | ||
You can serialize/deserialize request and response with JSON: | ||
```js | ||
var get = require('simple-get') | ||
var opts = { | ||
method: 'POST', | ||
url: 'http://example.com', | ||
body: { | ||
key: 'value' | ||
}, | ||
json: true | ||
} | ||
get.concat(opts, function (err, res, data) { | ||
if (err) throw err | ||
console.log(data.key) // `data` is an object | ||
}) | ||
``` | ||
## license | ||
MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org). |
@@ -386,1 +386,98 @@ var concat = require('concat-stream') | ||
}) | ||
test('simple get json', function (t) { | ||
t.plan(5) | ||
var server = http.createServer(function (req, res) { | ||
t.equal(req.url, '/path') | ||
t.equal(req.headers['content-type'], 'application/json') | ||
res.statusCode = 200 | ||
res.end('{"message":"response"}') | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
var opts = { | ||
url: 'http://localhost:' + port + '/path', | ||
json: true | ||
} | ||
get(opts, function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
res.pipe(concat(function (data) { | ||
t.equal(data.toString(), '{"message":"response"}') | ||
server.close() | ||
})) | ||
}) | ||
}) | ||
}) | ||
test('get.concat json', function (t) { | ||
t.plan(3) | ||
var server = http.createServer(function (req, res) { | ||
res.statusCode = 200 | ||
res.end('{"message":"response"}') | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
var opts = { | ||
url: 'http://localhost:' + port + '/path', | ||
json: true | ||
} | ||
get.concat(opts, function (err, res, data) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.equal(data.message, 'response') | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
test('get.concat json error', function (t) { | ||
t.plan(1) | ||
var server = http.createServer(function (req, res) { | ||
res.statusCode = 500 | ||
res.end('not json') | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
var opts = { | ||
url: 'http://localhost:' + port + '/path', | ||
json: true | ||
} | ||
get.concat(opts, function (err, res, data) { | ||
t.ok(err instanceof Error) | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
test('post (json body)', function (t) { | ||
t.plan(4) | ||
var server = http.createServer(function (req, res) { | ||
t.equal(req.method, 'POST') | ||
res.statusCode = 200 | ||
req.pipe(res) | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
var opts = { | ||
method: 'POST', | ||
url: 'http://localhost:' + port, | ||
body: { | ||
message: 'this is the body' | ||
}, | ||
json: true | ||
} | ||
get.concat(opts, function (err, res, data) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.equal(data.message, 'this is the body') | ||
server.close() | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19614
496
134