{"id":4561,"library":"harfile","title":"HAR File Writer","description":"harfile is a zero-dependency Python library designed for writing HTTP Archive (HAR) files. It provides a straightforward way to construct HAR-compliant logs of HTTP requests and responses programmatically. The library is currently at version 0.4.0 and maintains a stable API for its core functionality.","status":"active","version":"0.4.0","language":"python","source_language":"en","source_url":"https://github.com/schemathesis/harfile","tags":["har","http archive","http","logging","network","testing"],"install":[{"cmd":"pip install harfile","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"open","correct":"from harfile import open"},{"symbol":"Request","correct":"from harfile import Request"},{"symbol":"Response","correct":"from harfile import Response"},{"symbol":"Timings","correct":"from harfile import Timings"}],"quickstart":{"code":"import datetime\nimport io\nfrom harfile import open, Request, Response, Timings\n\n# Example 1: Write to a file\nwith open(\"example.har\") as har_file:\n    har_file.add_entry(\n        startedDateTime=datetime.datetime.now(datetime.timezone.utc),\n        time=42,\n        request=Request(\n            method=\"GET\",\n            url=\"http://example.com\",\n            httpVersion=\"HTTP/1.1\",\n        ),\n        response=Response(\n            status=200,\n            statusText=\"OK\",\n            httpVersion=\"HTTP/1.1\",\n        ),\n        timings=Timings(\n            send=0,\n            wait=0,\n            receive=0,\n        ),\n    )\n\n# Example 2: Write to a string buffer\nbuffer = io.StringIO()\nwith open(buffer) as har_buffer:\n    har_buffer.add_entry(\n        startedDateTime=datetime.datetime.now(datetime.timezone.utc),\n        time=100,\n        request=Request(\n            method=\"POST\",\n            url=\"http://api.example.com/data\",\n            httpVersion=\"HTTP/1.1\",\n            headers=[{\"name\": \"Content-Type\", \"value\": \"application/json\"}],\n            postData={\n                \"mimeType\": \"application/json\",\n                \"text\": \"{\\\"key\\\": \\\"value\\\"}\"\n            }\n        ),\n        response=Response(\n            status=201,\n            statusText=\"Created\",\n            httpVersion=\"HTTP/1.1\",\n            content={\n                \"mimeType\": \"application/json\",\n                \"text\": \"{\\\"status\\\": \\\"success\\\"}\"\n            }\n        ),\n        timings=Timings(\n            send=5,\n            wait=50,\n            receive=45,\n        ),\n    )\n\nprint(\"HAR file 'example.har' created.\")\nprint(\"HAR content written to string buffer (first 200 chars):\\n\", buffer.getvalue()[:200])\n","lang":"python","description":"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."},"warnings":[{"fix":"Ensure `harfile` operations are performed in a single-threaded context or implement external locking mechanisms if absolutely necessary within a multi-threaded application.","message":"The `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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"If 'pages' functionality is required, consider alternative HAR generation libraries or post-processing the `harfile` output to manually add page entries.","message":"The `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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Before 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.","message":"HAR 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.","severity":"gotcha","affected_versions":"All versions (inherent to HAR format)"}],"env_vars":null,"search_vec":"'0.4.0':42 'api':47 'archiv':16,54 'compliant':28 'construct':25 'core':50 'current':39 'depend':9 'design':12 'file':2,18 'function':51 'har':1,17,27,52 'har-compli':26 'harfil':4 'http':15,31,53,55 'librari':11,37 'log':29,56 'maintain':44 'network':57 'programmat':35 'provid':20 'python':10 'request':32 'respons':34 'stabl':46 'straightforward':22 'test':58 'version':41 'way':23 'write':14 'writer':3 'zero':8 'zero-depend':7","created_at":"2026-04-12T13:57:54.671866+00:00","updated_at":"2026-04-16T15:32:07.836464+00:00","problems":[{"fix":"Install the library using pip: `pip install harfile`","cause":"The 'harfile' library has not been installed in your Python environment or there's a typo in the import statement.","error":"ModuleNotFoundError: No module named 'harfile'"},{"fix":"Instantiate 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.","cause":"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.","error":"TypeError: 'harfile.Request' object is not callable"},{"fix":"Ensure 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(...))`.","cause":"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.","error":"TypeError: add_entry() missing 1 required positional argument: 'response'"},{"fix":"The correct method for adding entries in `harfile` is `add_entry`, not `write_entry`. Use `har.add_entry(...)`.","cause":"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.","error":"AttributeError: 'HarFile' object has no attribute 'write_entry'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.5.0","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/schemathesis/harfile","docs":null,"changelog":"https://github.com/schemathesis/harfile/blob/main/CHANGELOG.md","pypi":"https://pypi.org/project/harfile/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["http-networking","serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-30","next_check":"2026-07-28","install_tag":null}}