Giter VIP home page Giter VIP logo

simple-get's Introduction

simple-get travis npm downloads

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) // 200
  res.pipe(process.stdout) // `res` is a stream
})

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, data, res) {
  if (err) throw err
  console.log(res.statusCode) // 200
  console.log(data) // 'this is the server response'
})

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) // `res` is a stream
})

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',

  // simple-get accepts all options that node.js `http` accepts
  // See: http://nodejs.org/api/http.html#http_http_request_options_callback
  headers: {
    'user-agent': 'my cool app'
  }
}, function (err, res) {
  if (err) throw err

  // All properties/methods from http.IncomingResponse are available,
  // even if a gunzip/inflate transform stream was returned.
  // See: http://nodejs.org/api/http.html#http_http_incomingmessage
  res.setTimeout(10000)
  console.log(res.headers)

  res.pipe(concat(function (data) {
    // `data` is the decoded response, after it's been gunzipped or inflated
    // (if applicable)
    console.log('got the response: ' + data)
  }))

})

license

MIT. Copyright (c) Feross Aboukhadijeh.

simple-get's People

Contributors

feross avatar sindresorhus avatar sdtd-yngwie avatar

Watchers

James Cloos avatar Brett Lawson avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.