HTTPSnippet-lite
HTTPSnippet-lite is a JavaScript and TypeScript library designed to generate HTTP request code snippets across a wide array of programming languages and tools, including cURL, JavaScript (Fetch, Axios), Node.js (native, request), Python, Go, C#, Java, and more. It functions by taking a JSON object conforming to the HTTP Archive (HAR) format's HarRequest or HarEntry specification as input and converting it into executable code. Currently at version 3.0.5, the package is in active production use, offering a stable solution for developers needing versatile HTTP snippet generation. As a maintained fork of the original Kong/httpsnippet, `httpsnippet-lite` distinguishes itself by having no reliance on Node.js core modules, providing a leaner footprint, and explicitly marking its `convert` method as asynchronous. It does *not* perform HAR input validation, assuming the provided HAR is well-formed, which is a key difference from its predecessor.
Common errors
-
TypeError: httpsnippet_lite_1.HTTPSnippet is not a constructor
cause Attempting to use CommonJS `require` syntax or incorrect destructuring for an ESM-focused library, especially in TypeScript projects or modern Node.js environments.fixUse ESM named imports: `import { HTTPSnippet } from 'httpsnippet-lite';`. If operating in a pure CommonJS environment, ensure your build setup correctly handles ESM interop or verify `httpsnippet-lite`'s CJS export behavior. -
(node:...) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'convert' of undefined
cause Forgetting to `await` the asynchronous `snippet.convert()` method, leading to `convert` being called on an unresolved Promise.fixAlways `await` the result of `snippet.convert(...)`. For example: `const output = await snippet.convert('shell', 'curl', options);` -
Error: Invalid HAR format: Headers property must be an array
cause The input HAR object (e.g., `HarRequest` or `HarEntry`) contains malformed data, such as a non-array value for the `headers` property, which `httpsnippet-lite` does not validate.fixEnsure your HAR input strictly adheres to the HAR specification. For `headers`, it must be an array of objects, each with `name` and `value` properties. Review HAR documentation for correct structure.
Warnings
- breaking The `convert()` method is asynchronous and returns a Promise. Failing to `await` its result will lead to unhandled promise rejections or incorrect output.
- gotcha HTTPSnippet-lite does *not* perform validation on the input HAR (HTTP Archive) object. It assumes the provided `HarRequest` or `HarEntry` is strictly compliant with the HAR specification. Providing malformed HAR data can lead to unexpected output or runtime errors without explicit warnings.
- gotcha This package (`httpsnippet-lite`) is a fork of the original `Kong/httpsnippet` and is independently maintained. Key differences include no reliance on Node.js core modules, the `convert()` method being async, and the absence of HAR validation. Users migrating from or accustomed to the original `httpsnippet` should review the documentation for behavioral changes and feature set differences.
- breaking This package specifies a Node.js engine requirement of `>=14.13`. Running the library on older Node.js versions may result in compatibility issues, runtime errors, or unexpected behavior.
Install
-
npm install httpsnippet-lite -
yarn add httpsnippet-lite -
pnpm add httpsnippet-lite
Imports
- HTTPSnippet
const HTTPSnippet = require('httpsnippet-lite');import { HTTPSnippet } from 'httpsnippet-lite'; - HarRequest
import { HarRequest } from 'httpsnippet-lite';import type { HarRequest } from 'httpsnippet-lite'; - addTarget
const { addTarget } = require('httpsnippet-lite');import { addTarget } from 'httpsnippet-lite';
Quickstart
import { HTTPSnippet } from 'httpsnippet-lite';
const snippet = new HTTPSnippet({
method: 'GET',
url: 'http://mockbin.com/request',
headers: [
{ name: 'Accept', value: 'application/json' }
],
queryString: [
{ name: 'foo', value: 'bar' }
]
});
const options = { indent: '\t' };
const output = await snippet.convert('shell', 'curl', options);
console.log(output);
/* Expected output:
curl -X GET 'http://mockbin.com/request?foo=bar' \
-H 'Accept: application/json'
*/