Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
A micro ajax library modeled after angularjs's $http module that doesn't require any dependencies (jquery or otherwise). As of version 1.1.0, uhttp runs in browsers as well as in nodejs.
uhttp is about 6kb minified and about 2.3kb minified and compressed.
uhttp supports setting headers globally for all requests and setting headers individually for each request. It also automatically parses json in responses with the appropriate content type. uhttp is based off of atomic and angularjs's $http. uhttp was written because atomic didn't support common features (setting headers & sending json POST requests) and React didn't come with a built in ajax library (recommending jQuery's ajax instead).
Note that uhttp does not use true promises.
Download the minified build here, put into your public scripts directory, and add to your webpage by adding the following tag:
<script type="application/javascript" src="/scripts/uhttp.min.js"></script>
Alternatively, you can install from bower as well:
bower install uhttp --save
To install with nodejs, run the following:
npm install uhttp --save
Use server side by requiring in your files:
var uhttp = require('uhttp');
Use uhttp.get() to make a GET request. You can use either "then... catch" callbacks to obtain the response.
uhttp.get('/api/endpoint').then(function(res, status, xhr) {
//Successful response
}).catch(function(err, status, xhr) {
//Error
});
You can specify options by passing an options object to any request (see the Options section for more information):
var options = {
headers: {
'custom': 'header'
},
withCredentials: true,
timeout: 3000 //3 seconds; '0' for no timeout
};
uhttp.get('/api/endpoint', options).then(function(res, status, xhr) {
//Success
}).catch(function(err, status, xhr) {
//Error
});
Use uhttp.post() to make a POST request. By default, the content type for JSON and FormData (multipart/form-data NOT application/x-www-form-urlencoded) is automatically set for you. To change this, either set a global default for the "Content-Type" header, or pass it in as an option.
uhttp.post('/api/endpoint/post', {some: 'data'}).then(function(res) {
//Success
}).catch(function(err) {
//Error
});
Example using FormData (Content-Type: multipart/form-data) in javascript:
var formElement = document.getElementById("myform");
var formData = new FormData(formElement);
formData.append("username", "cat");
uhttp.post('/api/endpoint/post/form/multipart', formData).then(function(data) {
//Success
}).catch(function(err) {
//Error
});
Here is an example of posting x-www-form-urlencoded data. Note that if you want all requests sent with these options by default, use uhttp.setGlobalOptions (see documentation below).
var data = {
content: 'Sending json object as x-www-form-urlencoded'
};
var options = {
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequestData: function(data) {
var str = [];
for(var p in data) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(data[p]));
}
return str.join("&");
}};
uhttp.post('/api/endpoint/post/form/urlencoded', options, data).then(function(res) {
//Success
}).catch(function(err) {
//Error
});
Example setting a custom content type:
var myCustomData = '<custom>xml</custom>';
uhttp.post('/api/endpoint/post', {'Content-Type': 'application/xml'}, myCustomData).then(function(res, status, xhr) {
//Success
}).catch(function(err, status, xhr) {
//Error
});
Use uhttp.put() to make a PUT request. The options are similar to POST requests.
uhttp.put('/api/endpoint/put', {some: 'data'}).then(function(res, status, xhr) {
//Success
}).catch(function(err, status, xhr) {
//Error
});
Use uhttp.patch() to make a PATCH request.
uhttp.patch('/api/endpoint/patch', {some: 'data'}).then(function(res, status, xhr) {
//Success
}).catch(function(err, status, xhr) {
//Error
});
Use uhttp.delete() to send a DELETE request.
uhttp.delete('/api/endpoint/delete').then(function(res, status, xhr) {
//Success
}).catch(function(err, status, xhr) {
//Error
});
Use uhttp.head() to send a HEAD request.
uhttp.head('/api/endpoint/head').then(function(res, status, xhr) {
//Success
console.log(xhr.getHeader('Custom-Header'));
}).catch(function(err, status, xhr) {
//Error
});
Use uhttp.jsonp() to send a JSONP request. Note that you should define the callback as 'JSON_CALLBACK'. uhttp will generate a global function attached to the window object for the duration of the request and pass its data to the then/catch functions.
uhttp.jsonp('/api/endpoint/jsonp?callback=JSON_CALLBACK').then(function(res, status, xhr) {
//Success
}).catch(function(res, status, xhr) {
//Error
});
uhttp handles setting an XSRF header for you based on the options xsrfCookieName and xsrfHeaderName options. The default options are cookieName = 'XSRF-TOKEN' and headerName = 'X-XSRF-TOKEN'. Note that uhttp takes the value of the cookie (not the name) and sets the header given by xsrfHeaderName to that value. To change these, specify the options before sending your requests:
var globalOptions = {
xsrfCookieName: 'CUSTOM-COOKIE-NAME',
xsrfHeaderName: 'CUSTOM-HEADER-NAME'
};
uhttp.setGlobalOptions = globalOptions;
uhttp.get('/xsrf/endpoint').then(function(res) {
//Success
}).catch(function(err) {
//Error
});
A common use case for POSTing data is to send a file to your server (like an image or video file). uhttp makes that easy by allowing you to specify a progress callback with your POST request. Note that this example uses Xhr2, which is not supported by IE8. Check caniuse for more information on browser compatibility. For a full example, see the example in /test/testupload.html
var files = document.getElementById('upload-input-element').files;
var file = files[0];
var data = new FormData();
data.append('userfile', file);
var options = {
progressHandler: function(event) {
if(event.lengthComputable) {
var percentageLoaded = event.loaded / event.total;
document.findElementById('upload-progress-bar').setAttribute('style', 'width: ' + percentageLoaded + ';');
}
}
};
uhttp.post('/upload/endpoint', options, data).then(function(res) {
//Success
}).catch(function(err) {
//Error
});
If your requests require tokens, you can specify them as custom headers (also note that you can set these globally for all requests by calling uhttp.setGlobalOptions(globalOptions).
var MY_TOKEN = 'CUSTOM_AUTH_TOKEN';
var options = {
headers: {
'Authorization': 'Bearer ' + MY_TOKEN
}
};
uhttp.get('/protected/api/endpoint', options).then(function(res) {
//Success
}).catch(function(err) {
//Error
});
CORS is a way of sending requests to a different domain than the one you are currently browsing. CORS is enabled on a server by setting the appropriate headers on the server.
Server pseudo code:
response.header('Access-Control-Allow-Origin', 'http://example.com');
response.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
response.header('Access-Control-Allow-Headers', 'Content-Type');
response.header('Access-Control-Allow-Credentials', true);
Using uhttp, if the server has required credentials (by the 'Access-Control-Allow-Credentials' header), you would need to set the withCredentials option to correctly send your request. You would also need to set any authorization headers the server requires (tokens, etc.).
var MY_TOKEN = 'CUSTOM_AUTH_TOKEN';
var options = {
headers: {
'Authorization': 'Bearer ' + MY_TOKEN
},
withCredentials: true
};
uhttp.get('http://subdomain.example.com/protected/api/endpoint', options).then(function(res) {
//Success
}).catch(function(err) {
//Error
});
You can set global options by using the uhttp.setGlobalOptions(options) method. Note that "global" means on each page. If you are not using a single page web application architecture, you will need to set these options in a common script across all your pages. In a single page application architecture, this works as expected after setting once.
uhttp.setGlobalOptions({
headers: {
'Custom': 'Global-Header'
},
timeout: 2000, //Set timeout to 2 seconds
withCredentials: true, //Set withCredentials on xhr requests,
transformRequest: function(xhr) {}, //Transform xhr before sending (also before transformRequestData)
transformResponse: function(xhr) {}, //Transform xhr after response (but before transformResponseData)
transformRequestData: function(data) {return data;}, //Transform requests before sending
transformResponseData: function(data) {return data;}, //Transform returned responses
caching: true || [Cache object] || {cache: [Cache object], options: {timeout: 120000}}, //Set whether to globally cache all requests (not recommended - use individual request options instead)
xsrfCookieName: String, //The name of the cookie where you store your xsrfToken
xsrfHeaderName: String //The name of the header to set the xsrfToken
});
You can also retrieve the global default options by using the uhttp.getGlobalOptions() method
var globalAjaxOptions = uhttp.getGlobalOptions();
console.log(globalAjaxOptions);
The options object is the same as the globalOptions object above except it can be passed with each individual request.
var options = {
headers: {
'Custom': 'Header'
}
};
uhttp.get('/api/endpoint', options).then(function(res) {
//Success
}).catch(function(err) {
//Error
});
var options = {
timeout: 2000 //Timeout of 2 seconds
};
uhttp.get('/api/endpoint', options).then(function(res) {
//Success
}).catch(function(err) {
//Error
});
uhttp lets you transform requests and responses before they're sent / after their returned (globally or per request). This is useful if you need to modify how uhttp processes its requests by default.
To modify the xhr object that uhttp uses before sending, set the transformRequest option.
var options = {
transformRequest: function(config) {
//Changes the source of the request
config.src = 'http://localhost:43760/api/get/again';
return config;
},
transformResponse: function(xhr) {
//Changes the response
xhr.responseText = '{"data": "Man in the middle!"}';
}
};
window.uhttp.get('http://localhost:43760/api/get', options).then(function(res) {
console.log(res.data); //prints "Man in the middle!"
}).catch(function(err) {
//Do nothing
}).finally(function() {
done();
});
To modify the data that uhttp sends with the request, set the transformRequestData option. This is necessary if you're sending x-www-form-urlencoded data.
var data = {
content: 'Sending json object as x-www-form-urlencoded'
};
var options = {
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequestData: function(data) {
var str = [];
for(var p in data) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(data[p]));
}
return str.join("&");
}};
uhttp.post('/api/endpoint/post/form/urlencoded', options, data).then(function(res) {
//Success
}).catch(function(err) {
//Error
});
To modify the data that uhttp gets after getting a response, set the transformResponseData option.
//This is the default transformResponseData that uhttp uses to parse JSON responses
var options = {
transformResponseData: function(req) {
var result;
var d = req.responseText;
try {
result = JSON.parse(d);
} catch(e) {
result = d;
}
return result;
}
}
window.uhttp.get('http://localhost:43760/api/get', options).then(function(res) {
console.log(res.data);
}).catch(function(err) {
//Do nothing
}).finally(function() {
done();
});
uhttp provides a basic caching mechanism where you can cache your GET responses (no xhr request is sent). By default, setting the cache option to true enables this and uses uhttp's default cache. If you need a custom cache object, you can use uhttp's CacheFactory to create a custom cache object that you can manually clear and set timeouts for. Note: Caching only works for GET requests.
uhttp.get('http://localhost:43760/api/get', {cache: true}).then(function(res, status, xhr) {
//Sends xhr request
uhttp.get('http://localhost:43760/api/get', {cache: true}).then(function(res, status, xhr) {
//From javascript cache, no xhr request sent
}).catch(function(err) {
//Do nothing
});
}).catch(function(err) {
//Do nothing
});
Custom cache objects:
var cacheFactory = uhttp.CacheFactory;
var blogCache = cacheFactory.get('blogCache');
blogCache.put('firstpost','My blog');
blogCache.put('secondpost', 'stuff');
blogCache.delete('firstpost');
blogCache.get('secondpost');
blogCache.clear();
//You can pass this cache to uhttp's cache option as well!
uhttp.get('http://localhost:43760/api/get', {cache: blogCache}).then(function(res, status, xhr) {
//Data is cached in blogCache w/ url as key (you now have control of when to delete from the cache)
});
uhttp exposes its cookie helper functions as uhttp.getCookie() and uhttp.setCookie(). These can be helpful if you need to check or change cookie values in your app.
uhttp.setCookie('mycookie', 'yum'); //Sets mycookie to 'yum'
uhttp.setCookie('othercookie', 'yay!', 365); //Set cookie for 365 days
console.log(uhttp.getCookie('mycookie')); //'yum'
console.log(uhttp.getCookie('othercookie')); //'yay!'
uhttp is developed using a nodejs environment. Make sure that you have nodejs and npm installed, clone this source repository and run the following in the uhttp directory:
npm install && grunt build
That will install all dependencies for development, run uhttp's tests, and build a minified version of uhttp in the dist directory.
If you have bug fixes that you want merged into uhttp, submit a pull request on the github repository.
The MIT License (MIT)
Copyright (c) 2015 Suyog Sonwalkar
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
A micro ajax http library with a unified api for browsers and nodejs
The npm package uhttp receives a total of 1 weekly downloads. As such, uhttp popularity was classified as not popular.
We found that uhttp 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.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.