simple-get-es5

Simplest way to make http get requests, with an ES5 polyfill.
features
This module is designed to be the lightest possible wrapper on top of node.js http, but supporting:
- follows redirects
- automatically handles gzip/deflate responses
- supports HTTPS
- supports convenience
url key so there's no need to use url.parse on the url when specifying options
All this in < 100 lines of code.
install
npm install simple-get
usage
Note, all these examples also work in the browser with browserify.
simple GET request
Doesn't get easier than this:
var get = require('simple-get')
get('http://example.com', function (err, res) {
if (err) throw err
console.log(res.statusCode)
res.pipe(process.stdout)
})
even simpler GET request
If you just want the data, and don't want to deal with streams:
var get = require('simple-get')
get.concat('http://example.com', function (err, res, data) {
if (err) throw err
console.log(res.statusCode)
console.log(data)
})
POST, PUT, PATCH, HEAD, DELETE support
For POST, call get.post or use option { method: 'POST' }.
var get = require('simple-get')
var opts = {
url: 'http://example.com',
body: 'this is the POST body'
}
get.post(opts, function (err, res) {
if (err) throw err
res.pipe(process.stdout)
})
A more complex example:
var get = require('simple-get')
get({
url: 'http://example.com',
method: 'POST',
body: 'this is the POST body',
headers: {
'user-agent': 'my cool app'
}
}, function (err, res) {
if (err) throw err
res.setTimeout(10000)
console.log(res.headers)
res.on('data', function (chunk) {
console.log('got a chunk of the response: ' + chunk)
}))
})
JSON
You can serialize/deserialize request and response with JSON:
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)
})
Forms
You can send application/x-www-form-urlencoded form data:
var get = require('simple-get')
var opts = {
method: 'POST',
url: 'http://example.com',
form: {
key: 'value'
}
}
get.concat(opts, function (err, res, data) {})
license
MIT. Copyright (c) Feross Aboukhadijeh.