simple-get
Simplest way to make http get requests
features
This module is the lightest possible wrapper on top of node.js http
, but supporting these essential features:
- follows redirects
- automatically handles gzip/deflate responses
- supports HTTPS
- supports specifying a timeout
- supports convenience
url
key so there's no need to useurl.parse
on the url when specifying options - composes well with npm packages for features like cookies, proxies, form data, & OAuth
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:
const get =
even simpler GET request
If you just want the data, and don't want to deal with streams:
const get = get
POST, PUT, PATCH, HEAD, DELETE support
For POST
, call get.post
or use option { method: 'POST' }
.
const get = const opts = url: 'http://example.com' body: 'this is the POST body'get
A more complex example:
const get =
JSON
You can serialize/deserialize request and response with JSON:
const get = const opts = method: 'POST' url: 'http://example.com' body: key: 'value' json: trueget
Timeout
You can set a timeout (in milliseconds) on the request with the timeout
option.
If the request takes longer than timeout
to complete, then the entire request
will fail with an Error
.
const get = const opts = url: 'http://example.com' timeout: 2000 // 2 second timeout
One Quick Tip
It's a good idea to set the 'user-agent'
header so the provider can more easily
see how their resource is used.
const get = const pkg =
Proxies
You can use the tunnel
module with the
agent
option to work with proxies:
const get = const tunnel = const opts = url: 'http://example.com' agent: tunnel
Cookies
You can use the cookie
module to include
cookies in a request:
const get = const cookie = const opts = url: 'http://example.com' headers: cookie: cookie
Form data
You can use the form-data
module to
create POST request with form data:
const fs = const get = const FormData = const form = form const opts = url: 'http://example.com' body: form get
application/x-www-form-urlencoded
form data manually:
Or, include const get = const opts = url: 'http://example.com' form: key: 'value' get
OAuth
You can use the oauth-1.0a
module to create
a signed OAuth request:
const get = const crypto = const OAuth = const oauth = const token = key: processenvACCESS_TOKEN secret: processenvACCESS_TOKEN_SECRET const url = 'https://api.twitter.com/1.1/statuses/home_timeline.json' const opts = url: url headers: oauth json: true
Throttle requests
You can use limiter to throttle requests. This is useful when calling an API that is rate limited.
const simpleGet = const RateLimiter = RateLimiterconst limiter = 1 'second' const get = limiterget limiter var opts = url: 'http://example.com' getget { if err throw err console}
license
MIT. Copyright (c) Feross Aboukhadijeh.