Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

httpplease

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

httpplease

The polite HTTP request library for node and the browser

  • 0.5.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
17K
decreased by-5.51%
Maintainers
1
Weekly downloads
 
Created
Source

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)
  • Supports cross-domain requests in IE9 transparently with the oldiexdomain plugin

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

The request object supports the following properties:

NameDescription
urlThe URL to request.
methodThe HTTP method to use for the request.
bodyThe body to send with the request.
headers An object containing HTTP headers to send, for example: {Accept: '*/*'}.
errorOn404 A boolean specifying whether a 404 response should be treated as an error or not. Defaults to true.

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:

statusThe numeric status code.
textThe raw response text.
body The processed response body. Depending on the content type of the response and the plugins being used, this may be the same as `response.text` (a string) or some other object (like a parsed JSON object).
contentTypeThe content type of the response.
headersAn object containing the parsed response headers.
isHttpError A boolean that specifies whether this object represents a server-reported HTTP error. This may be false—even on error objects—in the case of non-HTTP errors like XDomain failures or plugin errors.
requestAn object representing the request.
xhrThe XHR or XDomain object used to make the request.

Plugins

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

NameEnabled by Default?Description
jsonparserNo Converts JSON responses into JS objects on response.body.
cleanurlYes Encodes unencoded characters in the request URL. Required by some browsers if you're using non-ASCII characters.
oldiexdomainNo Enables cross domain requests in IE9 by (transparently) using the XDomainRequest object when necessary.
oldieactivexNo For super old versions of IE that didn't define XMLHttpRequest, use an ActiveX object.

Plugins are enabled with the use method:

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

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 jsonparser = httppleaseplugins.jsonparser;
httpplease = httpplease.use(jsonparser);

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(jsonparser);

http
  .use(oldiexdomain)
  .get('http://example.com', function(err, res) { ... }); // Uses "jsonparser" plugin and "oldiexdomain".
http.get('http://example.com', function(err, res) { ... }); // Only uses "jsonparser" 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(jsonparser, 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:

MethodDescription
createXHR(req) Creates an XHR object. The first plugin that return a non-null value from this method will be used.
processRequest(req) This method gives the plugin a chance to manipulate the request object before the request is made. For example, it can change the body or add headers.
processResponse(res) This method gives the plugin a chance to manipulate the response object before the callback is invoked.

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!

Keywords

FAQs

Package last updated on 29 Apr 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc