Install & Compatibility
Where this runs
tested against v0.5.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.152s · 17.8MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.5s · import 0.132s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
open
✓ from harfile import open
Request
✓ from harfile import Request
Response
✓ from harfile import Response
Timings
✓ from harfile import Timings
This quickstart demonstrates how to create a HAR file by adding HTTP entries. It shows examples of writing to a physical file and an in-memory string buffer, including basic request and response details.
import datetime
import io
from harfile import open, Request, Response, Timings
# Example 1: Write to a file
with open("example.har") as har_file:
har_file.add_entry(
startedDateTime=datetime.datetime.now(datetime.timezone.utc),
time=42,
request=Request(
method="GET",
url="http://example.com",
httpVersion="HTTP/1.1",
),
response=Response(
status=200,
statusText="OK",
httpVersion="HTTP/1.1",
),
timings=Timings(
send=0,
wait=0,
receive=0,
),
)
# Example 2: Write to a string buffer
buffer = io.StringIO()
with open(buffer) as har_buffer:
har_buffer.add_entry(
startedDateTime=datetime.datetime.now(datetime.timezone.utc),
time=100,
request=Request(
method="POST",
url="http://api.example.com/data",
httpVersion="HTTP/1.1",
headers=[{"name": "Content-Type", "value": "application/json"}],
postData={
"mimeType": "application/json",
"text": "{\"key\": \"value\"}"
}
),
response=Response(
status=201,
statusText="Created",
httpVersion="HTTP/1.1",
content={
"mimeType": "application/json",
"text": "{\"status\": \"success\"}"
}
),
timings=Timings(
send=5,
wait=50,
receive=45,
),
)
print("HAR file 'example.har' created.")
print("HAR content written to string buffer (first 200 chars):\n", buffer.getvalue()[:200])
Debug
Known issues
gotchaThe `harfile` library is designed with the assumption of a single-threaded environment. Using it in a multi-threaded application without proper synchronization could lead to unexpected behavior or data inconsistencies in the generated HAR file.fixEnsure `harfile` operations are performed in a single-threaded context or implement external locking mechanisms if absolutely necessary within a multi-threaded application.
affects: >=0.1.0
gotchaThe `harfile` library explicitly states that 'Pages are not supported'. This means the generated HAR file will not contain the 'pages' array, which is an optional but common top-level object in the HAR specification.fixIf 'pages' functionality is required, consider alternative HAR generation libraries or post-processing the `harfile` output to manually add page entries.
affects: >=0.1.0
gotchaHAR files, by their nature, can contain sensitive information such as authentication tokens, cookies, and request/response bodies. Users generating HAR files should be aware of this and exercise caution when sharing these files.fixBefore sharing a HAR file, thoroughly review its content and redact any sensitive data (e.g., Auth Bearer tokens, HttpOnly cookies, personal information in payloads). Consider using HAR sanitization tools if available.
affects: All versions (inherent to HAR format)
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'harfile'
The 'harfile' library has not been installed in your Python environment or there's a typo in the import statement.
fixInstall the library using pip: `pip install harfile`
TypeError: 'harfile.Request' object is not callable
This error occurs when trying to call a HAR object attribute (like Request, Response, Timings) as a function, instead of instantiating it as a class.
fixInstantiate the HAR object attributes as classes: `request=harfile.Request(...)` instead of `request=harfile.Request(...)` if you accidentally use parentheses as if it were a function call.
TypeError: add_entry() missing 1 required positional argument: 'response'
The `add_entry` method requires specific arguments, and one or more of the mandatory arguments (e.g., `startedDateTime`, `time`, `request`, `response`, `timings`) were omitted or incorrectly passed.
fixEnsure all required arguments for `harfile.add_entry` are provided and correctly typed according to the library's API. For example: `har.add_entry(startedDateTime=datetime.datetime.now(datetime.timezone.utc), time=42, request=harfile.Request(...), response=harfile.Response(...), timings=harfile.Timings(...))`.
AttributeError: 'HarFile' object has no attribute 'write_entry'
This error likely occurs when attempting to call a method that does not exist on the `HarFile` object, or mistaking a method from a different HAR-related library for `harfile`'s API.
fixThe correct method for adding entries in `harfile` is `add_entry`, not `write_entry`. Use `har.add_entry(...)`.
Upgrade
Version history
0.5.0latest on PyPI · released May 29, 2026
Audit
Dependencies
No dependency data recorded yet.