HTTP Request Snippet Generator
httpsnippet is a JavaScript/TypeScript library that generates code snippets for HTTP requests across a wide array of programming languages and libraries from an HTTP Archive (HAR) object. It supports numerous targets, including cURL, JavaScript (Fetch, XMLHttpRequest, Axios), Node.js (request, Axios), Python (Requests), Java (OkHttp, Unirest), C#, Go, PHP, and more. The current stable version is 3.0.10, and the project maintains an active release cadence, frequently addressing dependency vulnerabilities, fixing bugs, and expanding support for new languages and clients. It is primarily used for developer tools, API documentation, and code generation, allowing users to easily translate network requests into runnable code examples.
Common errors
-
ReferenceError: window is not defined
cause Attempting to use `httpsnippet` in an SSR environment with an older version that accesses browser-specific globals.fixUpdate `httpsnippet` to version 3.0.9 or newer to fix SSR compatibility issues. -
ERR_REQUIRE_ESM
cause Trying to use `require()` to import `httpsnippet` in an ES Module project where `require` is not available or explicitly disallowed.fixUse ESM `import` syntax: `import { HTTPSnippet } from 'httpsnippet';`. -
TypeError: httpsnippet is not a constructor
cause Attempting to instantiate `httpsnippet` using a default import or incorrect named import.fixEnsure you are using the named export `HTTPSnippet`: `import { HTTPSnippet } from 'httpsnippet'; const snippet = new HTTPSnippet(har);` -
The package 'httpsnippet' requires Node.js version "^20".
cause Running the `httpsnippet` package on a Node.js runtime version older than 20.fixUpgrade your Node.js environment to version 20 or higher (e.g., using `nvm install 20` and `nvm use 20`).
Warnings
- breaking Version 2.0.0 dropped support for Node.js versions older than 10. Running on older environments will lead to runtime errors.
- breaking Starting with version 3.0.4, the library officially requires Node.js version 20 or higher. Running on earlier versions (e.g., Node.js 18) will cause an engine incompatibility error.
- gotcha Earlier versions of `httpsnippet` (prior to v3.0.9) had issues when used in Server-Side Rendering (SSR) environments due to direct access to the `window` object, particularly when handling `form-data`.
- gotcha The package frequently addresses critical and high-severity vulnerabilities in its dependencies. It is crucial to regularly update to the latest patch versions to mitigate security risks.
- gotcha The `convert` method expects specific `target` and `client` string identifiers (e.g., 'javascript', 'fetch'). Using incorrect or unsupported identifiers will result in a 'Target not found' or similar error.
Install
-
npm install httpsnippet -
yarn add httpsnippet -
pnpm add httpsnippet
Imports
- HTTPSnippet
const HTTPSnippet = require('httpsnippet')import { HTTPSnippet } from 'httpsnippet' - HTTPSnippet
import HTTPSnippet from 'httpsnippet'
import { HTTPSnippet } from 'httpsnippet' - HarRequest
import { HarRequest } from 'httpsnippet'import type { HarRequest } from 'httpsnippet/dist/src/har'
Quickstart
import { HTTPSnippet } from 'httpsnippet';
const harRequest = {
method: 'POST',
url: 'https://api.example.com/users',
headers: [
{ name: 'Content-Type', value: 'application/json' },
{ name: 'Authorization', value: `Bearer ${process.env.API_KEY ?? ''}` }
],
postData: {
mimeType: 'application/json',
text: JSON.stringify({ name: 'Jane Doe', email: 'jane.doe@example.com' })
}
};
const snippet = new HTTPSnippet(harRequest);
// Generate a JavaScript fetch snippet
const jsFetchCode = snippet.convert('javascript', 'fetch');
console.log('---- JavaScript (Fetch) Snippet ----');
console.log(jsFetchCode);
// Generate a cURL snippet
const curlCode = snippet.convert('shell', 'curl');
console.log('\n---- cURL Snippet ----');
console.log(curlCode);
// Generate a Python Requests snippet
const pythonRequestsCode = snippet.convert('python', 'requests');
console.log('\n---- Python (Requests) Snippet ----');
console.log(pythonRequestsCode);