tiny-json-http

repository·main·Indexed 41 days ago

https://github.com/brianleroux/tiny-json-http

A minimalist, zero-dependency HTTP client for handling JSON payloads via GET, POST, PUT, PATCH, and DELETE, optimized for lightweight environments like AWS Lambda. Version 7.5.1 supports both Node-style error-first callbacks and Promises for async/await syntax.

Tokens
1.2K
Snippets
7
Records
8
Agent score
2%

What's inside tiny-json-http

  1. Handle callback and promise responses

    main

    Depending on how you call the API, the response format differs:

    Using Callbacks If a callback is provided, it receives two arguments:

    1. err: A JavaScript Error object if the request failed.
    2. data: An object containing the response headers and body.

    Using Promises If no callback is provided, the method returns a Promise that resolves to the same data object ({ headers, body }) or rejects with an err.

  2. Example: Use tiny-json-http with Callbacks

    main

    To use the traditional Node-style callback API, pass a function as the second argument to the method. The function follows the (err, result) signature.

    var tiny = require('tiny-json-http')
    var url = 'http://www.randomkittengenerator.com'
    
    tiny.get({url}, function _get(err, result) {
      if (err) {
        console.log('ruh roh!', err)
      }
      else {
        console.log(result)
      }
    })
  3. Example: Use tiny-json-http with Async/Await

    main

    To use the Promise-based API, omit the callback argument and use await within an async function. Wrap the call in a try/catch block to handle errors.

    var tiny = require('tiny-json-http')
    var url = 'http://www.randomkittengenerator.com'
    
    ;(async function _iife() {
      try {
        console.log(await tiny.get({url}))
      } catch (err) {
        console.log('ruh roh!', err)
      }
    })();
  4. Use tiny-json-http API methods

    main

    The library provides methods for both reading and writing data. If you do not provide a callback function, the methods return a Promise, allowing for async/await syntax. Otherwise, they follow the Node-style error-first callback pattern.

    // Read methods
    tiny.get(options[, callback])
    tiny.head(options[, callback])
    tiny.options(options[, callback])
    
    // Write methods
    tiny.post(options[, callback])
    tiny.put(options[, callback])
    tiny.patch(options[, callback])
    tiny.del(options[, callback])
  5. Configure tiny-json-http options

    main

    Pass an options object to any tiny method to configure the request.

    • url (required): The target endpoint.
    • data: For write methods (post, put, patch, del), this provides form variables. For get, this provides query string variables.
    • headers: A key/value map for request headers (supports multipart/form-data for file uploads).
    • buffer: If set to true, the response body is returned as a buffer instead of a parsed JSON object.
    // Example configuration object
    {
      url: 'http://example.com',
      data: { key: 'value' },
      headers: { 'Authorization': 'Bearer token' },
      buffer: true
    }
  6. Use tiny-json-http for HTTP requests

    main

    The tiny-json-http module provides a set of high-level functions for performing standard HTTP methods. It distinguishes between read operations (GET, HEAD, OPTIONS) and write operations (POST, PUT, PATCH, DELETE).

    • Read methods: get(), head(), and options() are used to retrieve data or metadata without modifying the resource.
    • Write methods: post(), put(), patch(), del(), and delete() are used to create, update, or remove resources.
    const http = require('tiny-json-http');
    
    // Example usage (conceptual):
    // http.get(url)
    // http.post(url, data)
    // http.del(url)
  7. Reference the HTTP method functions in tiny-json-http

    main

    The following methods are exported by the module to handle specific HTTP verbs:

    get: _read.bind({}, 'GET'),
    head: _read.bind({}, 'HEAD'),
    options: _read.bind({}, 'OPTIONS'),
    post: _write.bind({}, 'POST'),
    put: _write.bind({}, 'PUT'),
    patch: _write.bind({}, 'PATCH'),
    del: _write.bind({}, 'DELETE'),
    delete: _write.bind({}, 'DELETE')