CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
README.md334 linesDownload Raw Back to simple-get
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/master4[ci-url]: https://github.com/feross/simple-get/actions5[npm-image]: https://img.shields.io/npm/v/simple-get.svg6[npm-url]: https://npmjs.org/package/simple-get7[downloads-image]: https://img.shields.io/npm/dm/simple-get.svg8[downloads-url]: https://npmjs.org/package/simple-get9[standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg10[standard-url]: https://standardjs.com11 12### Simplest way to make http get requests13 14## features15 16This module is the lightest possible wrapper on top of node.js `http`, but supporting these essential features:17 18- follows redirects19- automatically handles gzip/deflate responses20- supports HTTPS21- supports specifying a timeout22- supports convenience `url` key so there's no need to use `url.parse` on the url when specifying options23- composes well with npm packages for features like cookies, proxies, form data, & OAuth24 25All this in < 100 lines of code.26 27## install28 29```30npm install simple-get31```32 33## usage34 35Note, all these examples also work in the browser with [browserify](http://browserify.org/).36 37### simple GET request38 39Doesn't get easier than this:40 41```js42const get = require('simple-get')43 44get('http://example.com', function (err, res) {45  if (err) throw err46  console.log(res.statusCode) // 20047  res.pipe(process.stdout) // `res` is a stream48})49```50 51### even simpler GET request52 53If you just want the data, and don't want to deal with streams:54 55```js56const get = require('simple-get')57 58get.concat('http://example.com', function (err, res, data) {59  if (err) throw err60  console.log(res.statusCode) // 20061  console.log(data) // Buffer('this is the server response')62})63```64 65### POST, PUT, PATCH, HEAD, DELETE support66 67For `POST`, call `get.post` or use option `{ method: 'POST' }`.68 69```js70const get = require('simple-get')71 72const opts = {73  url: 'http://example.com',74  body: 'this is the POST body'75}76get.post(opts, function (err, res) {77  if (err) throw err78  res.pipe(process.stdout) // `res` is a stream79})80```81 82#### A more complex example:83 84```js85const get = require('simple-get')86 87get({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` accepts93  // See: http://nodejs.org/api/http.html#http_http_request_options_callback94  headers: {95    'user-agent': 'my cool app'96  }97}, function (err, res) {98  if (err) throw err99 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_incomingmessage103  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 inflated108    // (if applicable)109    console.log('got a chunk of the response: ' + chunk)110  }))111 112})113```114 115### JSON116 117You can serialize/deserialize request and response with JSON:118 119```js120const get = require('simple-get')121 122const opts = {123  method: 'POST',124  url: 'http://example.com',125  body: {126    key: 'value'127  },128  json: true129}130get.concat(opts, function (err, res, data) {131  if (err) throw err132  console.log(data.key) // `data` is an object133})134```135 136### Timeout137 138You can set a timeout (in milliseconds) on the request with the `timeout` option.139If the request takes longer than `timeout` to complete, then the entire request140will fail with an `Error`.141 142```js143const get = require('simple-get')144 145const opts = {146  url: 'http://example.com',147  timeout: 2000 // 2 second timeout148}149 150get(opts, function (err, res) {})151```152 153### One Quick Tip154 155It's a good idea to set the `'user-agent'` header so the provider can more easily156see how their resource is used.157 158```js159const get = require('simple-get')160const pkg = require('./package.json')161 162get('http://example.com', {163  headers: {164    'user-agent': `my-module/${pkg.version} (https://github.com/username/my-module)`165  }166})167```168 169### Proxies170 171You can use the [`tunnel`](https://github.com/koichik/node-tunnel) module with the172`agent` option to work with proxies:173 174```js175const get = require('simple-get')176const tunnel = require('tunnel')177 178const opts = {179  url: 'http://example.com',180  agent: tunnel.httpOverHttp({181    proxy: {182      host: 'localhost'183    }184  })185}186 187get(opts, function (err, res) {})188```189 190### Cookies191 192You can use the [`cookie`](https://github.com/jshttp/cookie) module to include193cookies in a request:194 195```js196const get = require('simple-get')197const cookie = require('cookie')198 199const opts = {200  url: 'http://example.com',201  headers: {202    cookie: cookie.serialize('foo', 'bar')203  }204}205 206get(opts, function (err, res) {})207```208 209### Form data210 211You can use the [`form-data`](https://github.com/form-data/form-data) module to212create POST request with form data:213 214```js215const fs = require('fs')216const get = require('simple-get')217const FormData = require('form-data')218const form = new FormData()219 220form.append('my_file', fs.createReadStream('/foo/bar.jpg'))221 222const opts = {223  url: 'http://example.com',224  body: form225}226 227get.post(opts, function (err, res) {})228```229 230#### Or, include `application/x-www-form-urlencoded` form data manually:231 232```js233const get = require('simple-get')234 235const opts = {236  url: 'http://example.com',237  form: {238    key: 'value'239  }240}241get.post(opts, function (err, res) {})242```243 244### Specifically disallowing redirects245 246```js247const get = require('simple-get')248 249const opts = {250  url: 'http://example.com/will-redirect-elsewhere',251  followRedirects: false252}253// res.statusCode will be 301, no error thrown254get(opts, function (err, res) {})255```256 257### Basic Auth258 259```js260const user = 'someuser'261const pass = 'pa$$word'262const encodedAuth = Buffer.from(`${user}:${pass}`).toString('base64')263 264get('http://example.com', {265  headers: {266    authorization: `Basic ${encodedAuth}`267  }268})269```270 271### OAuth272 273You can use the [`oauth-1.0a`](https://github.com/ddo/oauth-1.0a) module to create274a signed OAuth request:275 276```js277const get = require('simple-get')278const crypto  = require('crypto')279const OAuth = require('oauth-1.0a')280 281const oauth = OAuth({282  consumer: {283    key: process.env.CONSUMER_KEY,284    secret: process.env.CONSUMER_SECRET285  },286  signature_method: 'HMAC-SHA1',287  hash_function: (baseString, key) => crypto.createHmac('sha1', key).update(baseString).digest('base64')288})289 290const token = {291  key: process.env.ACCESS_TOKEN,292  secret: process.env.ACCESS_TOKEN_SECRET293}294 295const url = 'https://api.twitter.com/1.1/statuses/home_timeline.json'296 297const opts = {298  url: url,299  headers: oauth.toHeader(oauth.authorize({url, method: 'GET'}, token)),300  json: true301}302 303get(opts, function (err, res) {})304```305 306### Throttle requests307 308You 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```js311const simpleGet = require('simple-get')312const RateLimiter = require('limiter').RateLimiter313const limiter = new RateLimiter(1, 'second')314 315const get = (opts, cb) => limiter.removeTokens(1, () => simpleGet(opts, cb))316get.concat = (opts, cb) => limiter.removeTokens(1, () => simpleGet.concat(opts, cb))317 318var opts = {319  url: 'http://example.com'320}321 322get.concat(opts, processResult)323get.concat(opts, processResult)324 325function processResult (err, res, data) {326  if (err) throw err327  console.log(data.toString())328}329```330 331## license332 333MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org).334