| [81bc7da] | 1 | # simple-get [![ci][ci-image]][ci-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url]
|
|---|
| 2 |
|
|---|
| 3 | [ci-image]: https://img.shields.io/github/workflow/status/feross/simple-get/ci/master
|
|---|
| 4 | [ci-url]: https://github.com/feross/simple-get/actions
|
|---|
| 5 | [npm-image]: https://img.shields.io/npm/v/simple-get.svg
|
|---|
| 6 | [npm-url]: https://npmjs.org/package/simple-get
|
|---|
| 7 | [downloads-image]: https://img.shields.io/npm/dm/simple-get.svg
|
|---|
| 8 | [downloads-url]: https://npmjs.org/package/simple-get
|
|---|
| 9 | [standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg
|
|---|
| 10 | [standard-url]: https://standardjs.com
|
|---|
| 11 |
|
|---|
| 12 | ### Simplest way to make http get requests
|
|---|
| 13 |
|
|---|
| 14 | ## features
|
|---|
| 15 |
|
|---|
| 16 | This module is the lightest possible wrapper on top of node.js `http`, but supporting these essential features:
|
|---|
| 17 |
|
|---|
| 18 | - follows redirects
|
|---|
| 19 | - automatically handles gzip/deflate responses
|
|---|
| 20 | - supports HTTPS
|
|---|
| 21 | - supports specifying a timeout
|
|---|
| 22 | - supports convenience `url` key so there's no need to use `url.parse` on the url when specifying options
|
|---|
| 23 | - composes well with npm packages for features like cookies, proxies, form data, & OAuth
|
|---|
| 24 |
|
|---|
| 25 | All this in < 100 lines of code.
|
|---|
| 26 |
|
|---|
| 27 | ## install
|
|---|
| 28 |
|
|---|
| 29 | ```
|
|---|
| 30 | npm install simple-get
|
|---|
| 31 | ```
|
|---|
| 32 |
|
|---|
| 33 | ## usage
|
|---|
| 34 |
|
|---|
| 35 | Note, all these examples also work in the browser with [browserify](http://browserify.org/).
|
|---|
| 36 |
|
|---|
| 37 | ### simple GET request
|
|---|
| 38 |
|
|---|
| 39 | Doesn't get easier than this:
|
|---|
| 40 |
|
|---|
| 41 | ```js
|
|---|
| 42 | const get = require('simple-get')
|
|---|
| 43 |
|
|---|
| 44 | get('http://example.com', function (err, res) {
|
|---|
| 45 | if (err) throw err
|
|---|
| 46 | console.log(res.statusCode) // 200
|
|---|
| 47 | res.pipe(process.stdout) // `res` is a stream
|
|---|
| 48 | })
|
|---|
| 49 | ```
|
|---|
| 50 |
|
|---|
| 51 | ### even simpler GET request
|
|---|
| 52 |
|
|---|
| 53 | If you just want the data, and don't want to deal with streams:
|
|---|
| 54 |
|
|---|
| 55 | ```js
|
|---|
| 56 | const get = require('simple-get')
|
|---|
| 57 |
|
|---|
| 58 | get.concat('http://example.com', function (err, res, data) {
|
|---|
| 59 | if (err) throw err
|
|---|
| 60 | console.log(res.statusCode) // 200
|
|---|
| 61 | console.log(data) // Buffer('this is the server response')
|
|---|
| 62 | })
|
|---|
| 63 | ```
|
|---|
| 64 |
|
|---|
| 65 | ### POST, PUT, PATCH, HEAD, DELETE support
|
|---|
| 66 |
|
|---|
| 67 | For `POST`, call `get.post` or use option `{ method: 'POST' }`.
|
|---|
| 68 |
|
|---|
| 69 | ```js
|
|---|
| 70 | const get = require('simple-get')
|
|---|
| 71 |
|
|---|
| 72 | const opts = {
|
|---|
| 73 | url: 'http://example.com',
|
|---|
| 74 | body: 'this is the POST body'
|
|---|
| 75 | }
|
|---|
| 76 | get.post(opts, function (err, res) {
|
|---|
| 77 | if (err) throw err
|
|---|
| 78 | res.pipe(process.stdout) // `res` is a stream
|
|---|
| 79 | })
|
|---|
| 80 | ```
|
|---|
| 81 |
|
|---|
| 82 | #### A more complex example:
|
|---|
| 83 |
|
|---|
| 84 | ```js
|
|---|
| 85 | const get = require('simple-get')
|
|---|
| 86 |
|
|---|
| 87 | get({
|
|---|
| 88 | url: 'http://example.com',
|
|---|
| 89 | method: 'POST',
|
|---|
| 90 | body: 'this is the POST body',
|
|---|
| 91 |
|
|---|
| 92 | // simple-get accepts all options that node.js `http` accepts
|
|---|
| 93 | // See: http://nodejs.org/api/http.html#http_http_request_options_callback
|
|---|
| 94 | headers: {
|
|---|
| 95 | 'user-agent': 'my cool app'
|
|---|
| 96 | }
|
|---|
| 97 | }, function (err, res) {
|
|---|
| 98 | if (err) throw err
|
|---|
| 99 |
|
|---|
| 100 | // All properties/methods from http.IncomingResponse are available,
|
|---|
| 101 | // even if a gunzip/inflate transform stream was returned.
|
|---|
| 102 | // See: http://nodejs.org/api/http.html#http_http_incomingmessage
|
|---|
| 103 | res.setTimeout(10000)
|
|---|
| 104 | console.log(res.headers)
|
|---|
| 105 |
|
|---|
| 106 | res.on('data', function (chunk) {
|
|---|
| 107 | // `chunk` is the decoded response, after it's been gunzipped or inflated
|
|---|
| 108 | // (if applicable)
|
|---|
| 109 | console.log('got a chunk of the response: ' + chunk)
|
|---|
| 110 | }))
|
|---|
| 111 |
|
|---|
| 112 | })
|
|---|
| 113 | ```
|
|---|
| 114 |
|
|---|
| 115 | ### JSON
|
|---|
| 116 |
|
|---|
| 117 | You can serialize/deserialize request and response with JSON:
|
|---|
| 118 |
|
|---|
| 119 | ```js
|
|---|
| 120 | const get = require('simple-get')
|
|---|
| 121 |
|
|---|
| 122 | const opts = {
|
|---|
| 123 | method: 'POST',
|
|---|
| 124 | url: 'http://example.com',
|
|---|
| 125 | body: {
|
|---|
| 126 | key: 'value'
|
|---|
| 127 | },
|
|---|
| 128 | json: true
|
|---|
| 129 | }
|
|---|
| 130 | get.concat(opts, function (err, res, data) {
|
|---|
| 131 | if (err) throw err
|
|---|
| 132 | console.log(data.key) // `data` is an object
|
|---|
| 133 | })
|
|---|
| 134 | ```
|
|---|
| 135 |
|
|---|
| 136 | ### Timeout
|
|---|
| 137 |
|
|---|
| 138 | You can set a timeout (in milliseconds) on the request with the `timeout` option.
|
|---|
| 139 | If the request takes longer than `timeout` to complete, then the entire request
|
|---|
| 140 | will fail with an `Error`.
|
|---|
| 141 |
|
|---|
| 142 | ```js
|
|---|
| 143 | const get = require('simple-get')
|
|---|
| 144 |
|
|---|
| 145 | const opts = {
|
|---|
| 146 | url: 'http://example.com',
|
|---|
| 147 | timeout: 2000 // 2 second timeout
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | get(opts, function (err, res) {})
|
|---|
| 151 | ```
|
|---|
| 152 |
|
|---|
| 153 | ### One Quick Tip
|
|---|
| 154 |
|
|---|
| 155 | It's a good idea to set the `'user-agent'` header so the provider can more easily
|
|---|
| 156 | see how their resource is used.
|
|---|
| 157 |
|
|---|
| 158 | ```js
|
|---|
| 159 | const get = require('simple-get')
|
|---|
| 160 | const pkg = require('./package.json')
|
|---|
| 161 |
|
|---|
| 162 | get('http://example.com', {
|
|---|
| 163 | headers: {
|
|---|
| 164 | 'user-agent': `my-module/${pkg.version} (https://github.com/username/my-module)`
|
|---|
| 165 | }
|
|---|
| 166 | })
|
|---|
| 167 | ```
|
|---|
| 168 |
|
|---|
| 169 | ### Proxies
|
|---|
| 170 |
|
|---|
| 171 | You can use the [`tunnel`](https://github.com/koichik/node-tunnel) module with the
|
|---|
| 172 | `agent` option to work with proxies:
|
|---|
| 173 |
|
|---|
| 174 | ```js
|
|---|
| 175 | const get = require('simple-get')
|
|---|
| 176 | const tunnel = require('tunnel')
|
|---|
| 177 |
|
|---|
| 178 | const opts = {
|
|---|
| 179 | url: 'http://example.com',
|
|---|
| 180 | agent: tunnel.httpOverHttp({
|
|---|
| 181 | proxy: {
|
|---|
| 182 | host: 'localhost'
|
|---|
| 183 | }
|
|---|
| 184 | })
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | get(opts, function (err, res) {})
|
|---|
| 188 | ```
|
|---|
| 189 |
|
|---|
| 190 | ### Cookies
|
|---|
| 191 |
|
|---|
| 192 | You can use the [`cookie`](https://github.com/jshttp/cookie) module to include
|
|---|
| 193 | cookies in a request:
|
|---|
| 194 |
|
|---|
| 195 | ```js
|
|---|
| 196 | const get = require('simple-get')
|
|---|
| 197 | const cookie = require('cookie')
|
|---|
| 198 |
|
|---|
| 199 | const opts = {
|
|---|
| 200 | url: 'http://example.com',
|
|---|
| 201 | headers: {
|
|---|
| 202 | cookie: cookie.serialize('foo', 'bar')
|
|---|
| 203 | }
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | get(opts, function (err, res) {})
|
|---|
| 207 | ```
|
|---|
| 208 |
|
|---|
| 209 | ### Form data
|
|---|
| 210 |
|
|---|
| 211 | You can use the [`form-data`](https://github.com/form-data/form-data) module to
|
|---|
| 212 | create POST request with form data:
|
|---|
| 213 |
|
|---|
| 214 | ```js
|
|---|
| 215 | const fs = require('fs')
|
|---|
| 216 | const get = require('simple-get')
|
|---|
| 217 | const FormData = require('form-data')
|
|---|
| 218 | const form = new FormData()
|
|---|
| 219 |
|
|---|
| 220 | form.append('my_file', fs.createReadStream('/foo/bar.jpg'))
|
|---|
| 221 |
|
|---|
| 222 | const opts = {
|
|---|
| 223 | url: 'http://example.com',
|
|---|
| 224 | body: form
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | get.post(opts, function (err, res) {})
|
|---|
| 228 | ```
|
|---|
| 229 |
|
|---|
| 230 | #### Or, include `application/x-www-form-urlencoded` form data manually:
|
|---|
| 231 |
|
|---|
| 232 | ```js
|
|---|
| 233 | const get = require('simple-get')
|
|---|
| 234 |
|
|---|
| 235 | const opts = {
|
|---|
| 236 | url: 'http://example.com',
|
|---|
| 237 | form: {
|
|---|
| 238 | key: 'value'
|
|---|
| 239 | }
|
|---|
| 240 | }
|
|---|
| 241 | get.post(opts, function (err, res) {})
|
|---|
| 242 | ```
|
|---|
| 243 |
|
|---|
| 244 | ### Specifically disallowing redirects
|
|---|
| 245 |
|
|---|
| 246 | ```js
|
|---|
| 247 | const get = require('simple-get')
|
|---|
| 248 |
|
|---|
| 249 | const opts = {
|
|---|
| 250 | url: 'http://example.com/will-redirect-elsewhere',
|
|---|
| 251 | followRedirects: false
|
|---|
| 252 | }
|
|---|
| 253 | // res.statusCode will be 301, no error thrown
|
|---|
| 254 | get(opts, function (err, res) {})
|
|---|
| 255 | ```
|
|---|
| 256 |
|
|---|
| 257 | ### Basic Auth
|
|---|
| 258 |
|
|---|
| 259 | ```js
|
|---|
| 260 | const user = 'someuser'
|
|---|
| 261 | const pass = 'pa$$word'
|
|---|
| 262 | const encodedAuth = Buffer.from(`${user}:${pass}`).toString('base64')
|
|---|
| 263 |
|
|---|
| 264 | get('http://example.com', {
|
|---|
| 265 | headers: {
|
|---|
| 266 | authorization: `Basic ${encodedAuth}`
|
|---|
| 267 | }
|
|---|
| 268 | })
|
|---|
| 269 | ```
|
|---|
| 270 |
|
|---|
| 271 | ### OAuth
|
|---|
| 272 |
|
|---|
| 273 | You can use the [`oauth-1.0a`](https://github.com/ddo/oauth-1.0a) module to create
|
|---|
| 274 | a signed OAuth request:
|
|---|
| 275 |
|
|---|
| 276 | ```js
|
|---|
| 277 | const get = require('simple-get')
|
|---|
| 278 | const crypto = require('crypto')
|
|---|
| 279 | const OAuth = require('oauth-1.0a')
|
|---|
| 280 |
|
|---|
| 281 | const oauth = OAuth({
|
|---|
| 282 | consumer: {
|
|---|
| 283 | key: process.env.CONSUMER_KEY,
|
|---|
| 284 | secret: process.env.CONSUMER_SECRET
|
|---|
| 285 | },
|
|---|
| 286 | signature_method: 'HMAC-SHA1',
|
|---|
| 287 | hash_function: (baseString, key) => crypto.createHmac('sha1', key).update(baseString).digest('base64')
|
|---|
| 288 | })
|
|---|
| 289 |
|
|---|
| 290 | const token = {
|
|---|
| 291 | key: process.env.ACCESS_TOKEN,
|
|---|
| 292 | secret: process.env.ACCESS_TOKEN_SECRET
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | const url = 'https://api.twitter.com/1.1/statuses/home_timeline.json'
|
|---|
| 296 |
|
|---|
| 297 | const opts = {
|
|---|
| 298 | url: url,
|
|---|
| 299 | headers: oauth.toHeader(oauth.authorize({url, method: 'GET'}, token)),
|
|---|
| 300 | json: true
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | get(opts, function (err, res) {})
|
|---|
| 304 | ```
|
|---|
| 305 |
|
|---|
| 306 | ### Throttle requests
|
|---|
| 307 |
|
|---|
| 308 | You can use [limiter](https://github.com/jhurliman/node-rate-limiter) to throttle requests. This is useful when calling an API that is rate limited.
|
|---|
| 309 |
|
|---|
| 310 | ```js
|
|---|
| 311 | const simpleGet = require('simple-get')
|
|---|
| 312 | const RateLimiter = require('limiter').RateLimiter
|
|---|
| 313 | const limiter = new RateLimiter(1, 'second')
|
|---|
| 314 |
|
|---|
| 315 | const get = (opts, cb) => limiter.removeTokens(1, () => simpleGet(opts, cb))
|
|---|
| 316 | get.concat = (opts, cb) => limiter.removeTokens(1, () => simpleGet.concat(opts, cb))
|
|---|
| 317 |
|
|---|
| 318 | var opts = {
|
|---|
| 319 | url: 'http://example.com'
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | get.concat(opts, processResult)
|
|---|
| 323 | get.concat(opts, processResult)
|
|---|
| 324 |
|
|---|
| 325 | function processResult (err, res, data) {
|
|---|
| 326 | if (err) throw err
|
|---|
| 327 | console.log(data.toString())
|
|---|
| 328 | }
|
|---|
| 329 | ```
|
|---|
| 330 |
|
|---|
| 331 | ## license
|
|---|
| 332 |
|
|---|
| 333 | MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org).
|
|---|