Skip to content

A powerful Python tool that converts HAR (HTTP Archive) files into executable Python code using popular HTTP libraries like `requests` and `httpx`.

License

Notifications You must be signed in to change notification settings

lisoboss/har2code

Repository files navigation

har2code

A powerful Python tool that converts HAR (HTTP Archive) files into executable Python code using popular HTTP libraries like requests and httpx.

Overview

HAR2CODE parses HAR files generated by browser developer tools or network monitoring tools and converts them into ready-to-run Python scripts. This is especially useful for:

  • API testing and debugging
  • Converting browser network requests into automated scripts
  • Documentation of API interactions
  • Reproducing HTTP requests in development environments

Features

  • Multi-library support: Generate code for both requests and httpx
  • Complete request reconstruction: Preserves headers, cookies, query parameters, and request bodies
  • Form data handling: Supports application/x-www-form-urlencoded and multipart/form-data
  • File upload support: Automatically handles file uploads and saves binary content
  • JSON parsing: Intelligently parses JSON request/response bodies
  • Base64 decoding: Automatically decodes base64-encoded content
  • Binary file handling: Saves binary content to appropriate files with correct extensions
  • Response documentation: Includes response information in generated code
  • Timestamp tracking: Preserves timing information for debugging

Installation

From Git URL

pip install git+https://github.com/lisoboss/har2code.git

From Source

git clone https://github.com/lisoboss/har2code.git
cd har2code
pip install -e .

Development Installation

git clone https://github.com/lisoboss/har2code.git
cd har2code
pip install -e ".[dev]"

Requirements

  • Python 3.12+
  • dacite - For data class instantiation
  • httpx - HTTP client library (optional, for generated code)
  • requests - HTTP client library (optional, for generated code)

Usage

Basic Usage

har2code input.har

Specify HTTP Library

# Generate code using requests (default)
har2code input.har --library requests

# Generate code using httpx
har2code input.har --library httpx

Save Output to File

har2code input.har > output.py

Generated Code Examples

Using requests

import requests

############## ======== HAR2CODE ======== ###############
# timestamp: 1723001234.567
# time: 245, 2024-08-07T10:30:34.567Z
# datetime: 2024-08-07T10:30:34.567Z
#

url = "https://api.example.com/users"
headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer token123'}
cookies = None
params = {'page': '1', 'limit': '10'}
data = None
json = {'name': 'John Doe', 'email': 'john@example.com'}
files = None

response = requests.request("POST", url, headers=headers, cookies=cookies, params=params, data=data, json=json, files=files)

"""
Response: HTTP/1.1 201 Created
Content-Type: application/json
Content-Length: 45

{"id": 123, "name": "John Doe", "created": true}
"""

Using httpx

import httpx

############## ======== HAR2CODE ======== ###############
# timestamp: 1723001234.567
# time: 245, 2024-08-07T10:30:34.567Z
# datetime: 2024-08-07T10:30:34.567Z
#

url = "https://api.example.com/users"
headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer token123'}
cookies = None
params = {'page': '1', 'limit': '10'}
data = None
json = {'name': 'John Doe', 'email': 'john@example.com'}
files = None

with httpx.Client() as client:
    response = client.request("POST", url, headers=headers, cookies=cookies, params=params, data=data, json=json, files=files)

"""
Response: HTTP/1.1 201 Created
Content-Type: application/json
Content-Length: 45

{"id": 123, "name": "John Doe", "created": true}
"""

HAR File Sources

You can generate HAR files from:

  • Chrome DevTools: Network tab → Right-click → Save as HAR with content
  • Firefox Developer Tools: Network tab → Right-click → Save All As HAR
  • Charles Proxy: File → Export → HAR file
  • Fiddler: File → Export Sessions → HTTPArchive v1.2
  • Browser extensions: Various HAR capture extensions

File Handling

The tool automatically handles:

  • Binary files: Saved to out/ directory with appropriate extensions
  • Text content: Decoded and embedded in the generated code
  • Base64 content: Automatically decoded when possible
  • Multipart uploads: File content is extracted and saved separately

Project Structure

har2code/
├── pyproject.toml
├── README.md
├── src
│   └── har2code
│       ├── __init__.py
│       ├── main.py          # CLI interface
│       ├── mime.py          # MIME type handling
│       ├── models
│       │   ├── __init__.py
│       │   ├── code.py      # Python code data models
│       │   └── har.py       # HAR format data models
│       ├── parser.py        # HAR parsing logic
│       └── tostr.py         # Code generation
├── tests
│   └── test_main.py
├── out/                     # Generated binary files
└── uv.lock

Development

Running Tests

pytest

Code Quality

# Pre-commit hooks are configured
pre-commit run --all-files

# Manual linting
black src/
mypy src/

Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature-name
  3. Make your changes and ensure tests pass
  4. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Created by lisoboss

Acknowledgments

About

A powerful Python tool that converts HAR (HTTP Archive) files into executable Python code using popular HTTP libraries like `requests` and `httpx`.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages