CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-httpsnippet

HTTP Request snippet generator for multiple programming languages and tools

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

cli-usage.mddocs/

CLI Usage

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.

Installation

Install HTTPSnippet globally to use the CLI:

npm install -g httpsnippet

Or use it directly with npx:

npx httpsnippet <options>

Basic Usage

httpsnippet [harFile] --target <target> [options]

Command Structure

httpsnippet [harFilePath] --target <target> [--client <client>] [--output <directory>] [--options <json>]

Required Arguments

  • harFilePath: Path to HAR file containing HTTP requests
  • --target (-t): Target language for code generation

Optional Arguments

  • --client (-c): Specific client library (uses target default if not specified)
  • --output (-o): Output directory for generated files (prints to stdout if not specified)
  • --options (-x): JSON string with client-specific options

Examples

Basic Usage

Generate cURL command from HAR file:

httpsnippet request.har --target shell --client curl

Output:

curl --request GET \
  --url 'https://api.example.com/users' \
  --header 'accept: application/json'

Save to File

Generate Python code and save to file:

httpsnippet request.har --target python --client requests --output ./generated

This creates ./generated/request.py with the generated Python code.

Multiple Language Generation

Generate 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 ./snippets

Custom Options

Use 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"}'

HAR File Format

The CLI accepts HAR files in either format:

Single Request 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\"}"
  }
}

Full HAR Entry Format

{
  "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\"}"
          }
        }
      }
    ]
  }
}

Available Targets and Clients

Use the library programmatically to list available targets:

const { availableTargets } = require('httpsnippet');
console.log(JSON.stringify(availableTargets(), null, 2));

Common Target/Client Combinations

TargetClientDescription
shellcurlcURL command
shellhttpieHTTPie command
javascriptfetchFetch API
javascriptaxiosAxios library
nodenativeNode.js http module
nodeaxiosNode.js with Axios
pythonrequestsPython requests library
javaokhttpOkHttp library
gonativeGo net/http
csharphttpclientC# HttpClient
phpcurlPHP cURL
rubynativeRuby Net::HTTP

Client-Specific Options

Different clients support different options for customizing generated code:

cURL Options

httpsnippet request.har --target shell --client curl --options '{
  "indent": "  ",
  "short": true,
  "prettifyJson": true,
  "insecureSkipVerify": false
}'

JavaScript/Node.js Options

httpsnippet request.har --target javascript --client fetch --options '{
  "indent": "\t"
}'

Python Options

httpsnippet request.har --target python --client requests --options '{
  "indent": "    "
}'

Output File Naming

When using --output, files are named based on the input HAR file and target:

  • Input: api-request.har
  • Target: python
  • Output: ./output-dir/api-request.py

The file extension is determined by the target's extname property.

Error Handling

The 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

Integration Examples

Build Script Integration

#!/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

CI/CD Pipeline

# 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/
    done

Programmatic CLI Access

Access 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

docs

cli-usage.md

core-api.md

helper-utilities.md

index.md

targets-and-clients.md

tile.json