A powerful Python tool that converts HAR (HTTP Archive) files into executable Python code using popular HTTP libraries like requests
and httpx
.
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
- Multi-library support: Generate code for both
requests
andhttpx
- Complete request reconstruction: Preserves headers, cookies, query parameters, and request bodies
- Form data handling: Supports
application/x-www-form-urlencoded
andmultipart/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
pip install git+https://github.com/lisoboss/har2code.git
git clone https://github.com/lisoboss/har2code.git
cd har2code
pip install -e .
git clone https://github.com/lisoboss/har2code.git
cd har2code
pip install -e ".[dev]"
- Python 3.12+
dacite
- For data class instantiationhttpx
- HTTP client library (optional, for generated code)requests
- HTTP client library (optional, for generated code)
har2code input.har
# Generate code using requests (default)
har2code input.har --library requests
# Generate code using httpx
har2code input.har --library httpx
har2code input.har > output.py
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}
"""
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}
"""
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
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
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
pytest
# Pre-commit hooks are configured
pre-commit run --all-files
# Manual linting
black src/
mypy src/
- Fork the repository
- Create a feature branch:
git checkout -b feature-name
- Make your changes and ensure tests pass
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
Created by lisoboss
- HAR specification: W3C HAR Format
- Built with dacite for dataclass instantiation