
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
browser-http
Advanced tools
Simple (but advanced) library for working with http in browser (like for example jQuery.ajax).
http-browser uses q promise pattern and is instance of EventEmitter. Can be used for example with simq.
$ npm install browser-http
var http = require('browser-http');
http.request('http://www.google.com', {type: 'GET'}).then(function(response) {
console.log(response.text);
}, function(e) {
throw e; // some error occurred
});
In then function, you will get response object with data from server.
var http = require('browser-http');
http.get('http://www.google.com');
http.post('http://www.google.com');
http.put('http://www.google.com');
http.delete('http://www.google.com');
In every http function, you can set other options. Now it is just type and data.
Basically it is just wrapper for some data from XMLHttpRequest.
If content-type in response header is application/json then your data will be automatically transformed into js object.
If you can not set this header on your server, than you can use *Json methods.
http.getJson('http://www.google.com/some.json').then(function(response) {
console.log(response.data); // output will be object
});
http.postJson('http://www.google.com/some.json');
By default all your requests are called from queue one by one, so there is always just one request running (or zero). Inspiration from this article http://blog.alexmaccaw.com/queuing-ajax-requests.
You can of course disable this behavior:
http.useQueue = false;
It is very easy to work with jsonp requests.
http.jsonp('http://some.url.com').then(function(response) {
console.log(response.data);
});
First, please read this discussion on stackoverflow.
Now if you want to use same technique just like Google or eg. Facebook do, you only need to set your own prefix in requests.
http.get('http://some.url.com', {
jsonPrefix: 'while(1);'
}).then(function(response) {
console.log(response.data);
});
String while(1); will be removed from the beginning of received data before parsing into json object.
You can listen for all http events with your own functions.
http.on('send', function(response, request) {
console.log('In any moment, new http request will be send to server');
});
http.on('complete', function(response, request) {
console.log('I just finished some request, but there may be some errors');
});
http.on('success', function(response, request) {
console.log('I have got response from server without any error :-)');
});
http.on('error', function(err, response, request) {
console.log('Sorry, there was some error with this response');
});
Sometimes it will be better to register whole group of events and this group is called extension.
http.addExtension('nameOfMyExtension', {
send: function(response, request) {},
complete: function(response, request) {},
success: function(response, request) {},
error: function(err, response, request) {},
});
You can also remove other extensions.
http.removeExtension('nameOfMyExtension');
browser-http already comes with few extensions. Originally they were created for projects build on Nette framework, but can be used on any other project.
new (require('browser-http/Extensions/Loading'));
Every time new request is send, your cursor is changed into progress cursor. After receiving response from server, cursor
is changed into auto.
new (require('browser-http/Extensions/Redirect'));
If your server sends json data with redirect variable, then you will be redirected to address in this variable.
var Snippets = require('browser-http/Extensions/Snippets');
new Snippets(window.jQuery);
If in response data is snippets object with html id and content pairs, then browser-http will iterate throw this object,
find element in page with given id and change content of this element into the one from given data.
This extension depends on jquery.
var Links = require('browser-http/Extensions/Links');
new Links(window.jQuery);
This is not true extension for browser-http. It listen for all click events on a links with class ajax but not with
class not-ajax and after this click, it creates ajax request.
Depends on jquery.
This is the same like the previous one, but apply for all forms with ajax class.
This extension can not handle forms with file uploads.
Depends on jquery.
var Forms = require('browser-http/Extensions/Forms');
new Forms(window.jQuery);
$ npm test
var http = require('browser-http/Mocks/Http');
afterEach(function() {
http.restore();
});
it('should load some data', function(done) {
http.receive('some data', {'content-type': 'text/plain'}, 200);
http.get('localhost').then(function(response) {
expect(response.data).to.be.equal('some data');
done();
});
});
// text/plain in headers list is default content-type, so you don't have to set it. Also status 200 is default.
it('should load some data and check received data', function(done) {
http.receive('some data', {'content-type': 'application/json'});
http.once('send', function(response, request) {
expect(request.xhr.url).to.be.equal('localhost?greeting=hello') // now we can test eg. url with parsed data
});
http.get('localhost', {data: {greeting: 'hello'}}).then(function(response) {
expect(response.data).to.be.eql({greeting: 'hello'});
done()
});
});
2.1.1
2.1.0
2.0.0
1.8.0
1.7.1
1.7.0
buildQuery and urlencode moved to browser-http/Helpers1.6.4
1.6.3
buildQuery - replaced with the real one from jQuery1.6.2
1.6.1
1.6.0
buildQuery should got the same output like jQuery.param1.5.2
1.5.1
1.5.0
1.4.0
1.3.1 - 1.3.5
1.3.0
urlencode and buildQuery methodsFAQs
Simple (but advanced) HTTP for browser
The npm package browser-http receives a total of 68 weekly downloads. As such, browser-http popularity was classified as not popular.
We found that browser-http demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.