HTTP Request/Response File Persistence
raw JSON →`http-file` is a TypeScript library designed for serializing and deserializing complete HTTP request and response objects to and from a structured file format. It provides core classes like `RequestFile` and `ResponseFile` to encapsulate all relevant HTTP interaction data, including method, URL, headers, and body, along with utility functions such as `readRequestFile`, `writeRequestFile`, `readResponseFile`, and `writeResponseFile` for file system operations. This capability is particularly useful for scenarios requiring the persistence of HTTP traffic, such as creating mock servers from recorded interactions, replaying requests for testing or debugging, or sharing specific HTTP payloads. The package is currently at version 1.0.3. While it ships with full TypeScript types and offers a stable API, its last release was over two years ago, indicating a mature, low-cadence maintenance or stable phase rather than active feature development. Its key differentiator lies in its focused approach to structured file-based storage of entire HTTP dialogues, distinct from general-purpose HTTP clients or static file servers.
Common errors
error Error: ENOENT: no such file or directory, open 'path/to/non-existent-file.json' ↓
fs.mkdir(path, { recursive: true }) for directories. error TypeError: The "path" argument must be of type string. Received type object ↓
Warnings
gotcha Storing sensitive HTTP request or response data (e.g., authorization tokens, user credentials, private data in bodies) directly to files on disk can be a security risk. Ensure these files are stored in secure locations with appropriate access controls and are not committed to version control systems. ↓
gotcha The library primarily operates on file paths. Incorrect or invalid file paths (e.g., non-existent directories, permission issues) will result in standard Node.js file system errors (e.g., `ENOENT`, `EACCES`) rather than custom library errors. These errors will be thrown by the underlying `fs/promises` module. ↓
gotcha The `body` property for `RequestFile` and `ResponseFile` supports `string | null | Buffer`. When dealing with non-UTF-8 textual content or binary data, it is crucial to pass a `Buffer` instance to preserve data integrity. Storing binary data as a plain string might lead to corruption or incorrect interpretation. ↓
Install
npm install http-file yarn add http-file pnpm add http-file Imports
- RequestFile wrong
const RequestFile = require('http-file').RequestFile;correctimport { RequestFile } from 'http-file'; - readRequestFile wrong
import readRequestFile from 'http-file/readRequestFile';correctimport { readRequestFile } from 'http-file'; - ResponseFile wrong
import * as httpFile from 'http-file'; const myResponse = new httpFile.ResponseFile();correctimport { ResponseFile, writeResponseFile } from 'http-file';
Quickstart
import { RequestFile, ResponseFile, writeRequestFile, readRequestFile, writeResponseFile, readResponseFile } from 'http-file';
import * as fs from 'fs/promises'; // For file cleanup
async function exampleUsage() {
const requestFilePath = 'example-request.http.json';
const responseFilePath = 'example-response.http.json';
// 1. Create a RequestFile instance
const requestData: RequestFile = {
method: 'GET',
url: 'https://api.example.com/data/items/1?cache=false',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${process.env.API_KEY ?? 'YOUR_FALLBACK_TOKEN'}`, // Use env var for sensitive data
},
body: null,
};
// 2. Write the RequestFile to disk
await writeRequestFile(requestFilePath, requestData);
console.log(`Request file written to ${requestFilePath}`);
// 3. Read the RequestFile from disk
const loadedRequest = await readRequestFile(requestFilePath);
console.log('Loaded request URL:', loadedRequest.url);
// 4. Create a ResponseFile instance
const responseData: ResponseFile = {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'X-Request-ID': 'unique-abc-123',
'Cache-Control': 'no-cache'
},
body: JSON.stringify({ message: 'Data fetched successfully', data: { id: 1, name: 'Item 1' } }),
};
// 5. Write the ResponseFile to disk
await writeResponseFile(responseFilePath, responseData);
console.log(`Response file written to ${responseFilePath}`);
// 6. Read the ResponseFile from disk
const loadedResponse = await readResponseFile(responseFilePath);
console.log('Loaded response status:', loadedResponse.statusCode);
console.log('Loaded response body:', loadedResponse.body ? JSON.parse(loadedResponse.body.toString()) : null);
// Clean up created files
await fs.unlink(requestFilePath);
await fs.unlink(responseFilePath);
console.log('Cleaned up example files.');
}
exampleUsage().catch(console.error);