{"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":"en","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,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}