npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

httpplease

v0.16.4

Published

The polite HTTP request library for node and the browser

Downloads

79,591

Readme

HTTP, Please

There are a lot of JS libraries for making HTTP requests in JavaScript. Why use this one? Because it's awesome, that's why. And this is why it's awesome:

  • Designed for "isomorphic" JavaScript (supporting both client and server with the same codebase)
  • …but with a browser-driven focus that keeps in mind the limitations of that environment (filesize, old IE)
  • Extensible via a simple but powerful plugin system (which it dogfoods)

browserify and webpack users can simply npm install httpplease.

Bower users can bower install httpplease.

<script> tag fans can grab the standalone build from the "browser-builds" directory.

Minified and gzipped, the standalone browser build is <2K.

API

Making a request

httpplease.get('http://example.com', function (err, res) {
    // Do something with the result.
});

Alternatively, you can pass a request options object as the first parameter:

httpplease.get({url: 'http://example.com'}, function (err, res) {
    // Do something with the result.
});

If you'd rather include the method in the object, that's okay too:

httpplease({method: 'GET', url: 'http://example.com'}, function (err, res) {
    // Do something with the result.
});

You can create a new http function with default request object values:

var http = httpplease.defaults({method: 'GET', errorOn404: false});
http('http://example.com', function (err, res) {
    // This request was made using the defaults specified above.
});

The request object

When you call one of the http functions (http, http.get, etc.), the result is a Request object. This object can also be processed by plugins. It has the same properties as the request options object you pass to the http function (though properties that you didn't pass will be filled by defaults). Those properties are:

The error object

In the event of an error, an error object will be passed as the first argument to your callback. If the error is an HTTP error, it will have all of the properties that a response object has (listed below), but will be a JS Error object (which can be useful if relying on instanceof checks). It also has one additional property—message—which contains a description of the error.

The response object

The response object passed to your callback in the event of a successful request has the following properties:

Plugins

httpplease supports plugins for changing how requests are made. Some plugins are built in:

Plugins are enabled with the use method:

var json = require('httpplease/plugins/json');
httpplease = httpplease.use(json);

Or, if you're using the standalone build:

<script src="httpplease.js" type="text/javascript"></script>
<script src="httppleaseplugins.js" type="text/javascript"></script>
var json = httppleaseplugins.json;
httpplease = httpplease.use(json);

Notice that use returns a new httpplease instance. This is so that you can create multiple instances, each with their own plugins:

var http = httpplease.use(json);

http
  .use(oldiexdomain)
  .get('http://example.com', function (err, res) { ... }); // Uses "json" plugin and "oldiexdomain".
http.get('http://example.com', function (err, res) { ... }); // Only uses "json" plugin.
httpplease.get('http://example.com', function (err, res) { ... }); // No extra plugins are used.

You can use as many plugins as you want—either by passing multiple plugins to use or chaining calls:

var http = httpplease
  .use(json, oldiexdomain, myPlugin)
  .use(anotherPlugin);

In order to keep your builds as small as possible, most plugins aren't enabled by default. (See the table above.) However, some small plugins are. If you want to disable all plugins, use the bare() method:

var http = httpplease.bare();

Like use(), this method also returns a new httpplease instance so you can continue to use the old object with the original plugins intact.

Custom Plugins

In addition to the bundled plugins, you can create your own. Plugins are simply objects that implement one or more of the following methods:

Similar Projects

Thanks

This project is mostly just a small wrapper around XMLHttpRequest and an (I hope) sensible structure for extending functionality. The reason it works on the server is because of driverdan's awesome node-XMLHttpRequest library—it's the secret sauce that makes the browser-focused design of httpplease possible!

Changelog

I try to write good commit messages so the commit log should be very readable, but here's a summary of some notable changes.

  • v0.16.0
    • Use onload on onerror where available since onreadystatechange can't differentiate between 0 status code errors and successes (matthewwithanm/react-inlinesvg#10)
  • v0.15.0
    • Rename "jsonparser" to "jsonresponse"
    • Set Accept header in "jsonresponse" instead of "jsonrequest"
    • Add "json" plugin that combines "jsonrequest" and "jsonresponse"
  • v0.14.0
    • Move plugins from httpplease/lib/plugins to httpplease/plugins
  • v0.13.1
    • Don't add Content-Type header if request has no body in jsonrequest plugin. (This avoids an unneeded preflight request.)
  • v0.13.0
    • Add support for timeouts
  • v0.12.0
    • Add abort to request object
  • v0.11.0
    • Add setprotocol plugin
  • v0.9.0
    • Ignore request headers when using oldiexdomain
    • Change when plugin methods are called
  • v0.8.1
    • Add Accept header if not present for jsonrequest
  • v0.8.0
    • Add onload and onerror
  • v0.7.0
    • Goodbye CoffeeScript
  • v0.6.0
    • Add jsonrequest plugin
    • Add header() to Request and Response objects
    • More tests
  • v0.5.3
    • Improve content-type matching
  • v0.5.1
    • Fix bug with non-404s being suppressed by errorOn404
  • v0.5.0
    • Return request object from http methods
  • v0.4.0
    • Added errorOn404 option
    • Make defaults a method
    • Make plugins a method
  • v0.3.0
    • Response-like errors
    • More tests
  • v0.2.0
    • Initial release as "httpplease"