HTTPSnippet provides a command-line interface for processing HAR files and generating code snippets with file output support. The CLI is useful for batch processing, integration with build systems, and automated code generation workflows.
Install HTTPSnippet globally to use the CLI:
npm install -g httpsnippetOr use it directly with npx:
npx httpsnippet <options>httpsnippet [harFile] --target <target> [options]httpsnippet [harFilePath] --target <target> [--client <client>] [--output <directory>] [--options <json>]harFilePath--target-t--client-c--output-o--options-xGenerate cURL command from HAR file:
httpsnippet request.har --target shell --client curlOutput:
curl --request GET \
--url 'https://api.example.com/users' \
--header 'accept: application/json'Generate Python code and save to file:
httpsnippet request.har --target python --client requests --output ./generatedThis creates
./generated/request.pyGenerate code for multiple languages:
# Generate cURL
httpsnippet request.har --target shell --client curl --output ./snippets
# Generate JavaScript fetch
httpsnippet request.har --target javascript --client fetch --output ./snippets
# Generate Python requests
httpsnippet request.har --target python --client requests --output ./snippetsUse client-specific options to customize output:
# cURL with custom indentation and short flags
httpsnippet request.har \
--target shell \
--client curl \
--options '{"indent": " ", "short": true}'
# JavaScript with specific formatting
httpsnippet request.har \
--target javascript \
--client fetch \
--options '{"indent": "\t"}'The CLI accepts HAR files in either format:
{
"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": "{\"name\":\"John\",\"email\":\"john@example.com\"}"
}
}{
"log": {
"version": "1.2",
"creator": {"name": "MyApp", "version": "1.0"},
"entries": [
{
"request": {
"method": "GET",
"url": "https://api.example.com/users",
"headers": [{"name": "accept", "value": "application/json"}]
}
},
{
"request": {
"method": "POST",
"url": "https://api.example.com/users",
"postData": {
"mimeType": "application/json",
"text": "{\"name\":\"Jane\"}"
}
}
}
]
}
}Use the library programmatically to list available targets:
const { availableTargets } = require('httpsnippet');
console.log(JSON.stringify(availableTargets(), null, 2));| Target | Client | Description |
|---|---|---|
| | cURL command |
| | HTTPie command |
| | Fetch API |
| | Axios library |
| | Node.js http module |
| | Node.js with Axios |
| | Python requests library |
| | OkHttp library |
| | Go net/http |
| | C# HttpClient |
| | PHP cURL |
| | Ruby Net::HTTP |
Different clients support different options for customizing generated code:
httpsnippet request.har --target shell --client curl --options '{
"indent": " ",
"short": true,
"prettifyJson": true,
"insecureSkipVerify": false
}'httpsnippet request.har --target javascript --client fetch --options '{
"indent": "\t"
}'httpsnippet request.har --target python --client requests --options '{
"indent": " "
}'When using
--outputapi-request.harpython./output-dir/api-request.pyThe file extension is determined by the target's
extnameThe CLI provides clear error messages for common issues:
# Invalid HAR file
httpsnippet invalid.har --target shell
# Error: Failed to parse HAR file
# Unknown target
httpsnippet request.har --target unknown
# Error: Unknown target 'unknown'
# Invalid options JSON
httpsnippet request.har --target shell --options 'invalid-json'
# Error: Failed to parse options JSON#!/bin/bash
# Generate API client code from HAR files
for har_file in tests/fixtures/*.har; do
base_name=$(basename "$har_file" .har)
# Generate multiple language clients
httpsnippet "$har_file" --target javascript --client fetch --output "clients/js/"
httpsnippet "$har_file" --target python --client requests --output "clients/python/"
httpsnippet "$har_file" --target go --client native --output "clients/go/"
done# GitHub Actions example
- name: Generate API Clients
run: |
npm install -g httpsnippet
mkdir -p generated-clients
for har in api-specs/*.har; do
httpsnippet "$har" --target shell --client curl --output generated-clients/
httpsnippet "$har" --target python --client requests --output generated-clients/
doneAccess CLI functionality programmatically:
/**
* Main CLI entry point function
*/
function go(): void;Usage Example:
// In Node.js script
const { go } = require('httpsnippet/dist/cli');
// Set process arguments
process.argv = [
'node',
'httpsnippet',
'request.har',
'--target', 'shell',
'--client', 'curl'
];
go(); // Executes CLI with provided arguments