What is simple-get?
The simple-get npm package is designed to provide a straightforward way to make HTTP requests in Node.js. It is built to be minimalistic but flexible, allowing users to perform GET, POST, and other HTTP methods with ease. It supports features like redirects, streaming, and simple error handling.
What are simple-get's main functionalities?
GET Request
This code sample demonstrates how to perform a simple GET request to a specified URL and stream the response to stdout. It also shows basic error handling.
const get = require('simple-get')
get('http://example.com', function (err, res) {
if (err) throw err
console.log(res.statusCode) // 200
res.pipe(process.stdout) // Stream the response to stdout
})
POST Request
This example illustrates how to send a POST request with a body of data. It demonstrates setting options for the request, including the URL, method, and body.
const get = require('simple-get')
const opts = {
url: 'http://example.com',
body: 'some data',
method: 'POST'
}
get(opts, function (err, res) {
if (err) throw err
console.log(res.statusCode) // 200
res.pipe(process.stdout) // Stream the response to stdout
})
Handling Redirects
This snippet shows how to handle redirects automatically by setting the 'followRedirects' option to true. It makes a request and follows any redirects, eventually logging the response status code of the final URL.
const get = require('simple-get')
get({ url: 'http://example.com', followRedirects: true }, function (err, res) {
if (err) throw err
console.log('Response from final URL:', res.statusCode)
res.pipe(process.stdout)
})
Other packages similar to simple-get
axios
Axios is a promise-based HTTP client for the browser and Node.js. It provides a more feature-rich API compared to simple-get, including support for interceptors, automatic transforms for JSON data, and client-side protection against XSRF. It is more suitable for complex applications that require detailed configuration of HTTP requests.
node-fetch
node-fetch is a light-weight module that brings the Fetch API to Node.js. It offers a similar simplicity to simple-get but is designed to closely mimic the browser's fetch API, making it ideal for developers familiar with front-end development. Unlike simple-get, node-fetch returns promises and supports the Fetch API's full feature set.
request
Request is a comprehensive HTTP client for Node.js that supports HTTPS, redirects, streams, and more. Although it has been deprecated, it was known for its rich feature set and flexibility. Compared to simple-get, request offered a more extensive set of options for configuring requests, making it suitable for complex scenarios.
simple-get
Simplest way to make http get requests
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
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')
var concat = require('concat-stream')
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.pipe(concat(function (data) {
console.log('got the response: ' + data)
}))
})
license
MIT. Copyright (c) Feross Aboukhadijeh.