Record a workflow, generate an OpenAPI spec from it, and write replay tests—provided the session cookies in the file never leak.
Key takeaways
- Enable Preserve log, walk through the business flow only once, replay one request with Copy as cURL, then export the full HAR file.
- Chrome strips Cookie, Set-Cookie, and Authorization headers by default when exporting HAR, so the file usually isn't enough to replay authenticated calls.
- mitmproxy2swagger generates OpenAPI 3.0 from a HAR file in two passes, and Playwright can replay the HAR so integration tests don't need to hit the customer portal.
When you export a HAR file from Chrome DevTools, the browser strips the Cookie, Set-Cookie, and Authorization request headers by default. This is intentional. The result is that a file just exported from a customer's internal portal may contain every request yet be unable to replay a single call that requires login.
This small detail captures the two sides of this technique. For a Forward Deployed Engineer (FDE), a HAR file is often the first API spec for a system no one has ever documented. It is also a file that would let someone impersonate the customer's users.
This guide walks through the entire process on a laptop: recording a workflow, replaying a request, extracting the endpoint list, generating an OpenAPI spec, and writing replay tests. Each step comes with practices for keeping the file safe. You'll need Chrome, Python 3, and a terminal with
curl
; if you want to complete the final step with Playwright, you'll also need a Node project.
The running example: an "Export report" button
Suppose a logistics customer wants your AI agent to automatically pull an inventory report every day. The report exists only behind an internal web portal. The customer's IT team confirms "there's an API," but no one still has the documentation.
No need to guess. Every time an employee clicks the button, the browser is calling the API—all you have to do is record those calls. HAR is a JSON format whose root is a
log
object, in which the
entries
array holds all the exported requests. Each element in the array is one call.
Before opening DevTools, ask the customer for a test account. Automated HAR redaction tools don't attempt to remove user information, so Okta recommends recording with a test account. If you record with a real employee's account, their name, email, and customer data will end up in the file.
Step 1: Enable Preserve log before logging in
Open DevTools, go to the Network panel, and check Preserve log. Without it, Chrome clears the request list on every page navigation, and login flows often redirect several times in a row. You would lose exactly the segment that shows how the session was established.
Then walk through the business flow in order, slowly: log in, open the report page, select the warehouse, click export. Don't click anything outside the flow. Every extra click becomes dozens of lines of noise you'll have to filter out later.
Check: even after the page has redirected several times, the top of the Network list still shows the login request.
Step 2: Replay one request before exporting the full file
Find the request that returns the report data, right-click, and choose Copy as cURL. Chrome also offers copying as a
fetch
call. This is the fastest way to turn a single call into a repeatable script.
Before running it, move the cookie value into an environment variable so it doesn't linger in your shell history or end up in a file you send to colleagues. The command below is simplified; the URL and cookie names are illustrative only:
export PORTAL_SESSION='...粘贴复制请求中的值...' curl 'https://portal.example.internal/api/reports/inventory?warehouse=HCM1' \ -H "Cookie: session=$PORTAL_SESSION" \ -H 'Accept: application/json'
Check: the response in the terminal should match the response in the Network panel exactly. If you get a 401, or get redirected back to the login page, the request depends on other headers—usually a CSRF token or a custom header. Add headers back one by one until the request succeeds, and note which ones are required.
Step 3: Export the HAR and figure out what's missing
Now export the whole flow as a HAR file. As noted at the start, Chrome omits Cookie, Set-Cookie, and Authorization by default on export. To include those headers, you must explicitly choose to export sensitive data.
Keep the default version. To understand the API's structure, what you need is URLs, methods, parameters, and response shapes—authentication was already figured out in Step 2. Only choose the full export if you genuinely need to replay the login flow itself, and only with a test account.
One more thing to note: the W3C draft of HAR was never published as a standard and has been abandoned. HAR is widely used without a formal standard, so different tools export slightly different fields. Code that reads HAR files should use
.get()
and not assume every field exists.
Step 4: Extract the endpoint list with a dozen lines of Python
A short flow can produce hundreds of requests, most of them images, fonts, and analytics scripts. The script below filters by the portal's host and groups calls by method and path:
# list_endpoints.py:用法:python3 -I list_endpoints.py portal.har portal.example.internal
import json, sys
from urllib.parse import urlsplit
with open(sys.argv[1], encoding="utf-8") as f:
har = json.load(f)
seen = {}
for e in har["log"]["entries"]:
req = e.get("request", {})
u = urlsplit(req.get("url", ""))
if u.netloc != sys.argv[2]:
continue
seen[(req.get("method"), u.path)] = e.get("response", {}).get("status")
for (method, path), status in sorted(seen.items()):
print(method, path, status)Check: the output should shrink to a few dozen lines. In the logistics example, you'll see at a glance which call fetches the warehouse list and which one exports the report. Those are exactly the two endpoints your AI agent needs.
Step 5: Let mitmproxy2swagger write the spec
The endpoint list is just a draft. The open-source tool mitmproxy2swagger can read a HAR file exported from DevTools and generate an OpenAPI 3.0 spec. For an internal API that has never had documentation, this is the first description both you and the customer's IT team can read.
The tool runs in two passes. The first pass lists the discovered paths, each with a
ignore:
prefix. You delete the prefixes of the paths you want to keep, then run the second pass to generate the description. The syntax below follows the current README; verify it against the version you installed:
mitmproxy2swagger -i portal.har -o inventory-api.yaml -p https://portal.example.internal/api # 打开 inventory-api.yaml,删除要保留的路径前的 'ignore:' mitmproxy2swagger -i portal.har -o inventory-api.yaml -p https://portal.example.internal/api
Check: open the YAML file and compare it with the output from Step 4. The spec is only accurate for what you clicked. Parameters that never appeared in the recorded flow won't be in the spec either, so record it again with a few different filter conditions.
Step 6: Write replay tests based on the HAR file
An undocumented API comes with no guarantees—the customer's IT team could change it next week. Playwright can record real traffic into a HAR file and replay it in tests, matching strictly by URL and method. This is great for writing integration tests for code that reads internal API responses. The snippet below is simplified:
// 首次运行:把真实流量录制到 HAR 文件
await page.routeFromHAR('har/inventory.har', { url: '**/api/**', update: true });
// 之后运行:去掉 update 即可回放,不再调用客户门户
await page.routeFromHAR('har/inventory.har', { url: '**/api/**' });The same HAR file has other uses. GitLab uses HAR files as input for API fuzzing in CI/CD. Fuzzing stresses the API being called—in this case, the customer's systems—so it should only be run against a test environment and only with their IT team's consent.
The most common mistake: file leakage
Technical mistakes are usually easy to fix: forgetting to enable Preserve log, missing a CSRF request header, assuming HAR fields always exist. What is hard to recover from is file leakage. HAR files record the request headers and cookies of every request, allowing a session to be reconstructed step by step. Cloudflare warns that a single session cookie may be enough for an attacker to impersonate a legitimate user.
So before sending the file to anyone, run it through a sanitization tool first, such as Cloudflare's HAR Sanitizer, which removes session-related cookies and JWTs. Okta requires removing API keys, secrets, cookie values, and passwords from traces before sharing them.
The easiest thing to forget is the test file from step 6. Playwright uses
update: true
to record HAR files, which capture real traffic, so they also store the valid session cookies of the account used during recording.
GitLab points out that HAR files may contain tokens, API keys, and session cookies, and should be reviewed before committing. Sanitize the
har/
directory before every commit, and only record with test accounts.
Getting the integration working in the first week on-site
At the customer site, this skill lets you get the integration working in the first week instead of waiting for documentation that may never appear. It also opens up a well-timed conversation with the IT team: you go to them with an OpenAPI spec asking which endpoints are stable, rather than asking where the API is.
When reading FDE job postings, watch for phrases like legacy system integration, internal systems, or customer data. They often describe exactly this kind of work.
On your resume, tell the full story: building a spec from recorded traffic, adding replay tests, and establishing a file sanitization process before sharing. If you want to build a portfolio, repeat this process on a web application you are authorized to use, then publish the spec and tests, but not the HAR file.
In fact, the first spec for an internal portal is already sitting in the Network panel. The hard part is extracting it without exposing anyone's login session.
Was this article helpful?
Thanks for your feedback!