The HTTPSnippet class is the main interface for converting HTTP requests into code snippets. It accepts HAR (HTTP Archive) format requests and provides methods to convert them into executable code for various programming languages.
Creates a new HTTPSnippet instance from HAR request data. Accepts either a single HAR request or a full HAR entry containing multiple requests.
/**
* Creates HTTPSnippet instance for generating code snippets
* @param input - HAR request object or HAR entry with multiple requests
*/
constructor(input: HarEntry | HarRequest);Usage Examples:
import { HTTPSnippet } from "httpsnippet";
// Single request
const snippet = new HTTPSnippet({
method: 'POST',
url: 'https://api.example.com/users',
headers: [
{ name: 'content-type', value: 'application/json' },
{ name: 'authorization', value: 'Bearer token123' }
],
postData: {
mimeType: 'application/json',
text: JSON.stringify({ name: 'John', email: 'john@example.com' })
}
});
// HAR entry with multiple requests
const harEntry = {
log: {
version: '1.2',
creator: { name: 'MyApp', version: '1.0' },
entries: [
{ request: { method: 'GET', url: 'https://api.example.com/users' } },
{ request: { method: 'POST', url: 'https://api.example.com/posts' } }
]
}
};
const multiSnippet = new HTTPSnippet(harEntry);Converts the HTTPSnippet requests into executable code for the specified target language and client library.
/**
* Convert requests to code snippets for specified target and client
* @param targetId - Target language identifier (e.g., 'javascript', 'python', 'shell')
* @param clientId - Optional client library identifier (uses default if not specified)
* @param options - Optional configuration for code generation
* @returns Generated code snippet(s) or false if conversion fails
*/
convert(targetId: TargetId, clientId?: ClientId, options?: any): string | string[] | false;Usage Examples:
import { HTTPSnippet } from "httpsnippet";
const snippet = new HTTPSnippet({
method: 'GET',
url: 'https://api.example.com/users',
headers: [{ name: 'accept', value: 'application/json' }]
});
// Basic conversion (uses default client)
const curlCode = snippet.convert('shell');
// Specific client
const fetchCode = snippet.convert('javascript', 'fetch');
// With options
const indentedCurl = snippet.convert('shell', 'curl', {
indent: ' ',
short: false
});
// For multiple requests, returns array
const multiSnippet = new HTTPSnippet(harEntryWithMultipleRequests);
const pythonCodes = multiSnippet.convert('python', 'requests');
// Returns: ['request1_code', 'request2_code', ...]Utility functions for type checking HAR format data.
/**
* Type guard to check if value is a valid HarEntry
* @param value - Value to check
* @returns True if value is HarEntry
*/
function isHarEntry(value: any): value is HarEntry;Usage Example:
import { isHarEntry } from "httpsnippet";
const data = JSON.parse(harFileContent);
if (isHarEntry(data)) {
const snippet = new HTTPSnippet(data);
// Process HAR entry...
} else {
// Handle as single request
const snippet = new HTTPSnippet(data as HarRequest);
}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; fileName?: string; contentType?: string }>;
};
bodySize?: number;
headersSize?: number;
}
interface HarEntry {
log: {
version: string;
creator: {
name: string;
version: string;
};
entries: Array<{
request: Partial<HarRequest>;
}>;
};
}
interface RequestExtras {
// Additional processing fields added to HAR requests
fullUrl: string;
queryObj: Record<string, any>;
headersObj: Record<string, string>;
cookiesObj: Record<string, string>;
allHeaders: Record<string, string>;
uriObj: {
protocol: string;
hostname: string;
pathname: string;
search: string;
query: Record<string, any>;
};
postData: {
mimeType: string;
text?: string;
params?: Array<{ name: string; value: string }>;
jsonObj?: Record<string, any>;
paramsObj?: Record<string, any>;
boundary?: string;
};
}
interface Request extends HarRequest {
// Extended request with processing extras
fullUrl: string;
queryObj: Record<string, any>;
headersObj: Record<string, string>;
cookiesObj: Record<string, string>;
allHeaders: Record<string, string>;
uriObj: {
protocol: string;
hostname: string;
pathname: string;
search: string;
query: Record<string, any>;
};
postData: {
mimeType: string;
text?: string;
params?: Array<{ name: string; value: string }>;
jsonObj?: Record<string, any>;
paramsObj?: Record<string, any>;
boundary?: string;
};
}
type TargetId = 'c' | 'clojure' | 'crystal' | 'csharp' | 'go' | 'http' | 'java' |
'javascript' | 'kotlin' | 'node' | 'objc' | 'ocaml' | 'php' | 'powershell' |
'python' | 'r' | 'ruby' | 'rust' | 'shell' | 'swift';
type ClientId = string;