lil-http: Tiny Browser HTTP Client
lil-http is a minimalist, full-featured HTTP client designed specifically for browser environments. It provides a simple, callback-based API for making HTTP requests (GET, POST, PUT, DELETE, PATCH, HEAD) using XMLHttpRequest (XHR) under the hood. Currently at version 0.1.17, the library prioritizes a tiny footprint (3 KB uncompressed, 1 KB gzipped) and broad browser compatibility (down to IE9, Chrome 5, Firefox 3) rather than modern features like Promises or the Fetch API. Its key differentiators were its small size and straightforward XHR wrapper API during its active development phase. However, the project appears to be unmaintained, with no recent releases or updates, making it unsuitable for new projects in modern web development stacks.
Common errors
-
TypeError: lil.http is undefined
cause The `lil` global object or `lil.http` property was not found. This usually happens when the library's script file (`http.js`) hasn't been loaded in the HTML before being accessed, or if CommonJS `require()` was attempted in an environment that doesn't support it and no global fallback was registered.fixEnsure `<script src="path/to/http.js"></script>` is placed before any code attempting to use `lil.http`. Alternatively, if using a bundler, ensure `const http = require('lil-http');` is correctly used and bundled. -
Error: Cannot perform the request: 0
cause This error message (with status 0) often indicates a network error, such as the server being unreachable, a DNS resolution failure, a browser preventing the request due to security policies (e.g., CORS), or the request being aborted before it completes. Status 0 is XHR's way of indicating the request didn't even get to the point of receiving an HTTP status code.fixCheck the network tab in browser developer tools for more specific errors. Verify the target URL is correct and accessible. Review browser console for CORS related errors. If the issue is CORS, the server configuration needs to be adjusted to allow requests from your origin, or a proxy should be used. -
Access to XMLHttpRequest at 'http://example.com/api' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
cause The browser's Cross-Origin Resource Sharing (CORS) policy is blocking the request because the server at `http://example.com` did not send the necessary `Access-Control-Allow-Origin` header allowing requests from `http://localhost:8080`.fixConfigure the server (`http://example.com`) to include the `Access-Control-Allow-Origin` header with your origin (`http://localhost:8080`) or `*` for all origins (less secure). For local development, a proxy server can be used to bypass CORS.
Warnings
- breaking The package is effectively abandoned, last published as 0.1.17. It relies on XMLHttpRequest (XHR) and a callback-based API, which are considered legacy in modern JavaScript development, favoring Fetch API and Promises/async-await. Integrating it into a modern codebase would require significant effort or polyfills to align with contemporary asynchronous patterns.
- gotcha lil-http is a browser-only library and does not function in Node.js environments. It's built on `XMLHttpRequest`, which is a browser Web API.
- gotcha The package uses a global fallback (`lil.http`) if `require()` is not available, which can lead to global namespace pollution or conflicts with other libraries using a similar `lil` object. This pattern is generally discouraged in modern module-based development.
- deprecated The README mentions installation via Bower and Component, which are package managers that are largely deprecated or unmaintained in favor of npm/yarn. While `lil-http` is available on npm, its primary suggested installation methods are outdated.
Install
-
npm install lil-http -
yarn add lil-http -
pnpm add lil-http
Imports
- http
import http from 'lil-http';
const http = require('lil-http'); - lil.http
import { http } from 'lil-http';// When loaded via a <script> tag lil.http.get('/api/data', ...); - http.get
import { get } from 'lil-http';require('lil-http').get('/api/resource', options, callback);
Quickstart
<html>
<head>
<title>lil-http Quickstart</title>
<!-- Assuming lil-http.js is loaded, e.g., from a CDN or local file -->
<script src="https://cdn.rawgit.com/lil-js/http/0.1.17/http.js"></script>
</head>
<body>
<h1>lil-http GET Request Example</h1>
<pre id="output"></pre>
<script>
// Mock API endpoint (in a real scenario, this would be a server endpoint)
// For demonstration, we'll simulate a response after a delay.
const mockApiResponse = { message: 'Hello from lil-http!', version: '0.1.17' };
// Simulate a server response for '/sample.json'
function simulateFetch(url, options, callback) {
console.log(`Simulating GET request to ${url} with options:`, options);
setTimeout(() => {
if (url === '/sample.json') {
callback(null, { status: 200, data: mockApiResponse, headers: {} });
} else {
callback({ status: 404, message: 'Not Found' }, null);
}
}, 500);
}
// Override lil.http.get for this example to use our simulator
// In a real browser environment, lil.http.get would make a real XHR.
lil.http.get = simulateFetch;
lil.http.get('/sample.json', {
auth: { user: 'guest', password: 'password' },
headers: { 'X-Requested-With': 'lil-http' }
}, function (err, res) {
const outputDiv = document.getElementById('output');
if (err) {
console.error('Request failed:', err);
outputDiv.textContent = 'Error: ' + (err.message || 'Unknown error') + ' (Status: ' + err.status + ')';
} else if (res.status === 200) {
console.log('Response data:', res.data);
outputDiv.textContent = 'Success! Data: ' + JSON.stringify(res.data, null, 2);
} else {
outputDiv.textContent = 'Unexpected status: ' + res.status;
}
});
</script>
</body>
</html>