API
String module
Section titled “String module”Parse and stringify HTTP messages:
import * as string from '@johnhenry/http-converter/string';
// Parse requestconst req = string.parseRequest(`POST /users HTTP/1.1Host: api.example.comContent-Type: application/json
{"name": "John Doe"}`);
// Parse responseconst res = string.parseResponse(`HTTP/1.1 201 CreatedContent-Type: application/jsonLocation: /users/123
{"id": 123, "name": "John Doe"}`);
// Stringify with absolute URL (default for absolute URLs)// stringifyRequest/stringifyResponse/stringify always return a Promiseconst httpString1 = await string.stringifyRequest({ method: 'GET', url: 'https://www.example.com/path'});// GET https://www.example.com/path HTTP/1.1// host: www.example.com
// Stringify with path onlyconst httpString2 = await string.stringifyRequest({ method: 'GET', url: 'https://www.example.com/path'}, { absoluteUrl: false });// GET /path HTTP/1.1// host: www.example.comparse(httpString)— auto-detect and parse HTTP stringparseRequest(requestString)— parse HTTP request stringparseResponse(responseString)— parse HTTP response stringstringify(httpObject)— auto-detect and stringify HTTP object (async)stringifyRequest(request)— stringify HTTP request (async)stringifyResponse(response)— stringify HTTP response (async)
HAR module
Section titled “HAR module”Convert to/from HAR (HTTP Archive) format:
import * as har from '@johnhenry/http-converter/har';
// Convert request to HAR entry (fromRequest is async)const harEntry = await har.fromRequest({ method: 'GET', url: '/api/users', headers: { 'accept': 'application/json' }});
// Convert response to HAR (can merge with request); fromResponse is asyncconst completeEntry = await har.fromResponse(response, request);
// Convert back to HTTP objectsconst httpRequest = har.toRequest(harEntry);const httpResponse = har.toResponse(harEntry);fromRequest(request, options)— convert request to HAR entry (async)fromResponse(response, request, options)— convert response to HAR entry (async)toRequest(harEntry)— convert HAR entry to requesttoResponse(harEntry)— convert HAR entry to response
har.toResponse decodes base64-encoded HAR content (content.encoding === "base64", a spec-documented marker for binary/non-UTF8 bodies) rather than
returning the still-encoded blob as-is.
cURL module
Section titled “cURL module”Convert between cURL commands and HTTP requests:
import * as curl from '@johnhenry/http-converter/curl';
// Generate cURL command (fromRequest is async)const command = await curl.fromRequest({ method: 'POST', url: 'https://api.example.com/users', headers: { 'content-type': 'application/json' }, body: '{"name": "Jane"}'}, { pretty: true });
// Parse cURL commandconst request = curl.toRequest(`curl -X POST 'https://api.example.com/users' \ -H 'Content-Type: application/json' \ -d '{"name": "Jane"}'`);
// Generate fetch() codeconst fetchCode = curl.toFetchCode(command);fromRequest(request, options)— convert request to cURL command (async)toRequest(curlCommand)— parse cURL command to request, via a character-level tokenizer with correct single/double-quote and backslash-escape handling (including Chrome/Firefox’s “Copy as cURL” escaped-apostrophe pattern,'it'\''s') — throws on non-cURL input or an unterminated quote rather than silently mangling ittoFetchCode(curlCommand)— generatefetch()code from cURL
Fetch module
Section titled “Fetch module”Convert between Fetch API and HTTP objects:
import * as fetch from '@johnhenry/http-converter/fetch';
// Convert to Fetch parameters (fromRequest is async)const { url, options } = await fetch.fromRequest({ method: 'POST', url: '/api/users', headers: { 'content-type': 'application/json' }, body: '{"name": "Alice"}'});
// Use with fetch()const response = await fetch(url, options);
// Convert Response to HTTP objectconst httpResponse = await fetch.toResponse(response, true);
// Generate fetch() code (toCode is async)const code = await fetch.toCode(request, { pretty: true, async: true });fromRequest(request)— convert request to Fetch parameters (async)toRequest(url, options)— convert Fetch parameters to requestfromResponse(response, body)— convert response to Fetch-like objecttoResponse(fetchResponse, includeBody)— convert FetchResponseto HTTP object (async)toCode(request, options)— generatefetch()code (async); every interpolated string is built withJSON.stringify(), not manual quote wrapping, so a value containing a single quote (?q=O'Brien) still generates syntactically valid JavaScriptcreateMockResponse(httpResponse)— create mockResponseobject
Utilities
Section titled “Utilities”Available both as the root package export
(import { parseQueryString } from '@johnhenry/http-converter') and via
the @johnhenry/http-converter/core/utils subpath.
import { detectType, normalizeHeaders } from '@johnhenry/http-converter';
// Auto-detect format typedetectType('GET / HTTP/1.1'); // 'request'detectType('HTTP/1.1 200 OK'); // 'response'detectType('curl -X GET'); // 'curl'detectType({ log: {}, entries: [] }); // 'har'
// Normalize headers from various formatsnormalizeHeaders({ 'Content-Type': 'text/html' });normalizeHeaders([{ name: 'Content-Type', value: 'text/html' }]); // HAR formatnormalizeHeaders(new Headers({ 'Content-Type': 'text/html' })); // Fetch HeadersdetectType(input)— detect format typenormalizeHeaders(headers)— normalize headers to plain objectparseQueryString(url)— parse URL query parametersbuildUrl(baseUrl, queryParams)— build URL with query parametersgetByteSize(str)— calculate byte size of stringformatHeaders(headers)— format headers object as an HTTP header block
Body module
Section titled “Body module”Parse request/response bodies with automatic format detection:
import { parseBody } from '@johnhenry/http-converter/body';
const parsed = parseBody('{"hello":"world"}', 'application/json');// { type: 'json', formatted: '{\n "hello": "world"\n}', raw: '...' }parseBody(text, contentType?) detects JSON, XML, HTML, form-encoded, or
plain text, and returns { type, formatted, raw }.
All formats in one call
Section titled “All formats in one call”import { allFormats } from '@johnhenry/http-converter';const formats = await allFormats(request);// { httpString, curl, fetchCode, har }allFormats(request, options?) generates all format representations (HTTP
string, cURL, fetch code, HAR) for a request in one call.
Random module
Section titled “Random module”Generate random HTTP requests for testing, demos, and development:
import { randomRequest, randomMethod, randomPath, randomHeaders, randomBody } from '@johnhenry/http-converter/random';
// Generate a random requestconst req = randomRequest();// { method: 'POST', url: '/api/users/4217', headers: {...}, body: '{"name":"Alice",...}', httpVersion: '1.1' }
// Deterministic output with seedconst seeded = randomRequest({ seed: 42 });
// Generate multiple requestsconst batch = randomRequest({ count: 10 });
// Constrain methodsconst getOnly = randomRequest({ methods: ['GET'] });
// Custom optionsconst custom = randomRequest({ methods: ['POST', 'PUT'], baseUrl: 'https://api.example.com', headers: { 'authorization': 'Bearer my-token' }, body: { custom: 'payload' },});randomRequest(options?) returns an HttpRequest object (or array when
count is set). Seeded output (seed) is byte-identical across repeated
calls and round-trips cleanly through string/curl conversion.
Options:
seed(number) — deterministic PRNG seedmethods(string[]) — allowed HTTP methods (default: weighted pool favoring GET/POST)paths(string[] | function) — path templates with:idinterpolation, or generator(rng) => stringheaders(boolean | object) —true=generate,false=empty, object=use as-isbody(boolean | string | object | function) —true=auto (method-aware),false=none, string/object=use directly, function=(rng, method) => stringbaseUrl(string) — URL prefixcount(number) — return array of N requests
randomMethod(options?), randomPath(options?), randomHeaders(options?),
and randomBody(options?) are the individual generators behind
randomRequest, and accept the same relevant options.
Exports
Section titled “Exports”| Export | Description |
|---|---|
@johnhenry/http-converter |
Core: detectType, normalizeHeaders, allFormats, parseQueryString, buildUrl, getByteSize, formatHeaders |
@johnhenry/http-converter/string |
HTTP string parsing and stringification |
@johnhenry/http-converter/har |
HAR format conversion |
@johnhenry/http-converter/curl |
cURL command conversion |
@johnhenry/http-converter/fetch |
Fetch API conversion |
@johnhenry/http-converter/body |
Body parsing and formatting |
@johnhenry/http-converter/random |
Random HTTP request generation |