HTTPSnippet is a TypeScript library for generating HTTP request code snippets in multiple programming languages. It takes HTTP requests in HAR (HTTP Archive) format and converts them into executable code for 20+ language targets including cURL, JavaScript, Python, Java, Go, and many more.
npm install httpsnippetnpm install -g httpsnippetimport { HTTPSnippet } from "httpsnippet";For CommonJS:
const { HTTPSnippet } = require("httpsnippet");Additional imports:
import {
availableTargets,
addTarget,
addTargetClient,
extname,
isHarEntry,
targets,
CodeBuilder,
escapeString,
escapeForSingleQuotes,
escapeForDoubleQuotes,
escape,
getHeader,
getHeaderName,
hasHeader,
isMimeTypeJSON,
quote,
type HarRequest,
type HarEntry,
type Request,
type RequestExtras
} from "httpsnippet";import { HTTPSnippet } from "httpsnippet";
// Create snippet from HAR request
const snippet = new HTTPSnippet({
method: 'GET',
url: 'https://api.example.com/users',
headers: [
{ name: 'accept', value: 'application/json' },
{ name: 'authorization', value: 'Bearer token123' }
]
});
// Convert to different languages
const curl = snippet.convert('shell', 'curl');
const javascript = snippet.convert('javascript', 'fetch');
const python = snippet.convert('python', 'requests');
console.log(curl);
// Output: curl --request GET --url 'https://api.example.com/users' ...HTTPSnippet is built around several key components:
Main functionality for converting HAR requests into executable code snippets across multiple programming languages.
class HTTPSnippet {
constructor(input: HarEntry | HarRequest);
convert(targetId: TargetId, clientId?: ClientId, options?: any): string | string[] | false;
}
function isHarEntry(value: any): value is HarEntry;Access to all supported programming languages and their available library clients, plus functionality to extend with custom targets.
function availableTargets(): AvailableTarget[];
function addTarget(target: Target): void;
function addTargetClient(targetId: TargetId, client: Client): void;
function extname(targetId: TargetId): string;
const targets: Record<TargetId, Target>;
type TargetId = 'c' | 'clojure' | 'crystal' | 'csharp' | 'go' | 'http' | 'java' |
'javascript' | 'kotlin' | 'node' | 'objc' | 'ocaml' | 'php' | 'powershell' |
'python' | 'r' | 'ruby' | 'rust' | 'shell' | 'swift';Code building utilities, string escaping functions, and header processing tools for building and extending HTTPSnippet functionality.
class CodeBuilder {
constructor(options?: CodeBuilderOptions);
push(line: string, indentationLevel?: number): void;
join(): string;
}
function escapeString(rawValue: any, options?: EscapeOptions): string;
function getHeader<T>(headers: Record<string, T>, name: string): T | undefined;Command-line interface for processing HAR files and generating code snippets with file output support.
httpsnippet <harFile> --target <target> --client <client> --output <directory>interface HarRequest {
method: string;
url: string;
httpVersion?: string;
headers?: Array<{name: string, value: string}>;
queryString?: Array<{name: string, value: string}>;
cookies?: Array<{name: string, value: string}>;
postData?: {
mimeType: string;
text?: string;
params?: Array<{name: string, value: string}>;
};
}
interface HarEntry {
log: {
version: string;
creator: { name: string; version: string };
entries: Array<{ request: Partial<HarRequest> }>;
};
}
interface AvailableTarget {
key: TargetId;
title: string;
extname: string;
default: string;
clients: ClientInfo[];
}
interface ClientInfo {
key: string;
title: string;
link: string;
description: string;
}
interface RequestExtras {
fullUrl: string;
queryObj: Record<string, any>;
headersObj: Record<string, string>;
cookiesObj: Record<string, string>;
allHeaders: Record<string, string>;
uriObj: Record<string, any>;
postData: Record<string, any>;
}
type Request = HarRequest & RequestExtras;