source: node_modules/simple-get/README.md@ 81bc7da

finki-main main
Last change on this file since 81bc7da was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago

Initial commit

  • Property mode set to 100644
File size: 7.4 KB
Line 
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
16This 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
25All this in < 100 lines of code.
26
27## install
28
29```
30npm install simple-get
31```
32
33## usage
34
35Note, all these examples also work in the browser with [browserify](http://browserify.org/).
36
37### simple GET request
38
39Doesn't get easier than this:
40
41```js
42const get = require('simple-get')
43
44get('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
53If you just want the data, and don't want to deal with streams:
54
55```js
56const get = require('simple-get')
57
58get.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
67For `POST`, call `get.post` or use option `{ method: 'POST' }`.
68
69```js
70const 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 err
78 res.pipe(process.stdout) // `res` is a stream
79})
80```
81
82#### A more complex example:
83
84```js
85const 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` 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
117You can serialize/deserialize request and response with JSON:
118
119```js
120const get = require('simple-get')
121
122const opts = {
123 method: 'POST',
124 url: 'http://example.com',
125 body: {
126 key: 'value'
127 },
128 json: true
129}
130get.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
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 request
140will fail with an `Error`.
141
142```js
143const get = require('simple-get')
144
145const opts = {
146 url: 'http://example.com',
147 timeout: 2000 // 2 second timeout
148}
149
150get(opts, function (err, res) {})
151```
152
153### One Quick Tip
154
155It's a good idea to set the `'user-agent'` header so the provider can more easily
156see how their resource is used.
157
158```js
159const 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### Proxies
170
171You can use the [`tunnel`](https://github.com/koichik/node-tunnel) module with the
172`agent` option to work with proxies:
173
174```js
175const 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### Cookies
191
192You can use the [`cookie`](https://github.com/jshttp/cookie) module to include
193cookies in a request:
194
195```js
196const 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 data
210
211You can use the [`form-data`](https://github.com/form-data/form-data) module to
212create POST request with form data:
213
214```js
215const 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: form
225}
226
227get.post(opts, function (err, res) {})
228```
229
230#### Or, include `application/x-www-form-urlencoded` form data manually:
231
232```js
233const 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 redirects
245
246```js
247const get = require('simple-get')
248
249const opts = {
250 url: 'http://example.com/will-redirect-elsewhere',
251 followRedirects: false
252}
253// res.statusCode will be 301, no error thrown
254get(opts, function (err, res) {})
255```
256
257### Basic Auth
258
259```js
260const 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### OAuth
272
273You can use the [`oauth-1.0a`](https://github.com/ddo/oauth-1.0a) module to create
274a signed OAuth request:
275
276```js
277const 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_SECRET
285 },
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_SECRET
293}
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: true
301}
302
303get(opts, function (err, res) {})
304```
305
306### Throttle requests
307
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```js
311const simpleGet = require('simple-get')
312const RateLimiter = require('limiter').RateLimiter
313const 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 err
327 console.log(data.toString())
328}
329```
330
331## license
332
333MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org).
Note: See TracBrowser for help on using the repository browser.