lil-http: Tiny Browser HTTP Client

0.1.17 · abandoned · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic GET request using the globally exposed `lil.http` object, showing how to include authentication and custom headers, and handle the callback-based response.

<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>

view raw JSON →