HTTP Request snippet generator for multiple programming languages and tools
npx @tessl/cli install tessl/npm-httpsnippet@3.0.00
# HTTPSnippet
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: httpsnippet
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install httpsnippet`
10
- **CLI Installation**: `npm install -g httpsnippet`
11
12
## Core Imports
13
14
```typescript
15
import { HTTPSnippet } from "httpsnippet";
16
```
17
18
For CommonJS:
19
20
```javascript
21
const { HTTPSnippet } = require("httpsnippet");
22
```
23
24
Additional imports:
25
26
```typescript
27
import {
28
availableTargets,
29
addTarget,
30
addTargetClient,
31
extname,
32
isHarEntry,
33
targets,
34
CodeBuilder,
35
escapeString,
36
escapeForSingleQuotes,
37
escapeForDoubleQuotes,
38
escape,
39
getHeader,
40
getHeaderName,
41
hasHeader,
42
isMimeTypeJSON,
43
quote,
44
type HarRequest,
45
type HarEntry,
46
type Request,
47
type RequestExtras
48
} from "httpsnippet";
49
```
50
51
## Basic Usage
52
53
```typescript
54
import { HTTPSnippet } from "httpsnippet";
55
56
// Create snippet from HAR request
57
const snippet = new HTTPSnippet({
58
method: 'GET',
59
url: 'https://api.example.com/users',
60
headers: [
61
{ name: 'accept', value: 'application/json' },
62
{ name: 'authorization', value: 'Bearer token123' }
63
]
64
});
65
66
// Convert to different languages
67
const curl = snippet.convert('shell', 'curl');
68
const javascript = snippet.convert('javascript', 'fetch');
69
const python = snippet.convert('python', 'requests');
70
71
console.log(curl);
72
// Output: curl --request GET --url 'https://api.example.com/users' ...
73
```
74
75
## Architecture
76
77
HTTPSnippet is built around several key components:
78
79
- **HTTPSnippet Class**: Main class that processes HAR requests and converts them to code snippets
80
- **Target System**: Extensible architecture supporting 20+ programming languages (targets) with multiple library implementations (clients) per language
81
- **Helper Modules**: Utilities for code building, string escaping, and header processing
82
- **CLI Interface**: Command-line tool for batch processing and file output
83
- **Type System**: Full TypeScript support with comprehensive type definitions for HAR format and all targets
84
85
## Capabilities
86
87
### Core Snippet Generation
88
89
Main functionality for converting HAR requests into executable code snippets across multiple programming languages.
90
91
```typescript { .api }
92
class HTTPSnippet {
93
constructor(input: HarEntry | HarRequest);
94
convert(targetId: TargetId, clientId?: ClientId, options?: any): string | string[] | false;
95
}
96
97
function isHarEntry(value: any): value is HarEntry;
98
```
99
100
[Core API](./core-api.md)
101
102
### Language Targets and Clients
103
104
Access to all supported programming languages and their available library clients, plus functionality to extend with custom targets.
105
106
```typescript { .api }
107
function availableTargets(): AvailableTarget[];
108
function addTarget(target: Target): void;
109
function addTargetClient(targetId: TargetId, client: Client): void;
110
function extname(targetId: TargetId): string;
111
const targets: Record<TargetId, Target>;
112
113
type TargetId = 'c' | 'clojure' | 'crystal' | 'csharp' | 'go' | 'http' | 'java' |
114
'javascript' | 'kotlin' | 'node' | 'objc' | 'ocaml' | 'php' | 'powershell' |
115
'python' | 'r' | 'ruby' | 'rust' | 'shell' | 'swift';
116
```
117
118
[Targets and Clients](./targets-and-clients.md)
119
120
### Helper Utilities
121
122
Code building utilities, string escaping functions, and header processing tools for building and extending HTTPSnippet functionality.
123
124
```typescript { .api }
125
class CodeBuilder {
126
constructor(options?: CodeBuilderOptions);
127
push(line: string, indentationLevel?: number): void;
128
join(): string;
129
}
130
131
function escapeString(rawValue: any, options?: EscapeOptions): string;
132
function getHeader<T>(headers: Record<string, T>, name: string): T | undefined;
133
```
134
135
[Helper Utilities](./helper-utilities.md)
136
137
### CLI Interface
138
139
Command-line interface for processing HAR files and generating code snippets with file output support.
140
141
```bash
142
httpsnippet <harFile> --target <target> --client <client> --output <directory>
143
```
144
145
[CLI Usage](./cli-usage.md)
146
147
## Types
148
149
```typescript { .api }
150
interface HarRequest {
151
method: string;
152
url: string;
153
httpVersion?: string;
154
headers?: Array<{name: string, value: string}>;
155
queryString?: Array<{name: string, value: string}>;
156
cookies?: Array<{name: string, value: string}>;
157
postData?: {
158
mimeType: string;
159
text?: string;
160
params?: Array<{name: string, value: string}>;
161
};
162
}
163
164
interface HarEntry {
165
log: {
166
version: string;
167
creator: { name: string; version: string };
168
entries: Array<{ request: Partial<HarRequest> }>;
169
};
170
}
171
172
interface AvailableTarget {
173
key: TargetId;
174
title: string;
175
extname: string;
176
default: string;
177
clients: ClientInfo[];
178
}
179
180
interface ClientInfo {
181
key: string;
182
title: string;
183
link: string;
184
description: string;
185
}
186
187
interface RequestExtras {
188
fullUrl: string;
189
queryObj: Record<string, any>;
190
headersObj: Record<string, string>;
191
cookiesObj: Record<string, string>;
192
allHeaders: Record<string, string>;
193
uriObj: Record<string, any>;
194
postData: Record<string, any>;
195
}
196
197
type Request = HarRequest & RequestExtras;
198
```