Convert Fetch HTTP Requests to cURL
fetch-to-curl is a JavaScript/TypeScript utility library that generates `curl` commands from standard Web Fetch API inputs, including URLs, `Request` objects, and `RequestInit` options. Currently at version 0.6.0, it aims for simplicity and minimalism with zero external dependencies. Unlike libraries that patch the global `fetch` object, fetch-to-curl operates as a standalone wrapper, ensuring no side effects on your application's actual network requests. Releases are made periodically to address bug fixes and add minor enhancements, as seen with recent updates supporting URL objects and improving escaping. It is differentiated by its lightweight nature and its direct translation approach, making it ideal for debugging HTTP requests in development environments without modifying the runtime behavior of the application.
Common errors
-
TypeError: (0 , fetch_to_curl__WEBPACK_IMPORTED_MODULE_0__.fetchToCurl) is not a function
cause This error typically occurs in environments like Webpack or other bundlers when using an incorrect import syntax for a named export, often trying to import it as a default export or destructing incorrectly in a CJS context.fixEnsure you are using named import syntax: `import { fetchToCurl } from 'fetch-to-curl';` for ESM, or `const { fetchToCurl } = require('fetch-to-curl');` for CommonJS. Do not use `import fetchToCurl from 'fetch-to-curl';` as it is not a default export. -
ReferenceError: require is not defined
cause You are attempting to use CommonJS `require()` syntax in an ECMAScript Module (ESM) file (indicated by `"type": "module"` in `package.json` or a `.mjs` extension).fixChange your import statement to use ESM syntax: `import { fetchToCurl } from 'fetch-to-curl';`. If you must use CommonJS, ensure your file is treated as CJS (e.g., `.js` extension without `"type": "module"`). -
Output curl command is malformed or doesn't work as expected in terminal.
cause This can happen if you are using an older version of `fetch-to-curl` that had issues with character escaping in URLs or request bodies, or improper handling of certain header values.fixUpgrade `fetch-to-curl` to the latest version (`0.6.0` or newer) to benefit from fixes related to single quote escaping, URL object support, and robust handling of `null`/`undefined` headers. Always test the generated curl command after an upgrade.
Warnings
- gotcha Prior to version 0.5.1, `fetch-to-curl` might generate `curl` strings with improperly escaped URLs or request bodies, especially for strings containing single quotes or special characters. This could lead to malformed commands when executed in a terminal.
- gotcha Versions older than 0.6.0 had issues with missing single quote escaping for certain header values and did not correctly handle `null` or `undefined` headers, potentially leading to incorrect cURL output or errors.
- gotcha Although TypeScript types were improved in v0.5.2, relying on older versions in a TypeScript project might result in less accurate type inference or require more manual type assertions.
Install
-
npm install fetch-to-curl -
yarn add fetch-to-curl -
pnpm add fetch-to-curl
Imports
- fetchToCurl
import fetchToCurl from 'fetch-to-curl';
import { fetchToCurl } from 'fetch-to-curl'; - fetchToCurl (CommonJS)
const fetchToCurl = require('fetch-to-curl');const { fetchToCurl } = require('fetch-to-curl'); - Type definitions
import type { FetchToCurlOptions } from 'fetch-to-curl';
Quickstart
import { fetchToCurl } from 'fetch-to-curl';
// Example 1: Basic URL and options
const urlWithOptions = 'https://api.example.com/data';
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.API_TOKEN ?? ''}`
},
body: JSON.stringify({ key: 'value', id: 123 }),
};
console.log('--- cURL from URL and options ---');
console.log(fetchToCurl(urlWithOptions, options));
// Example 2: Using a Request object
const requestObject = new Request('https://api.example.com/users', {
method: 'GET',
headers: new Headers({
'Accept': 'application/xml',
'X-Custom-Header': 'Hello'
}),
});
console.log('\n--- cURL from Request object ---');
console.log(fetchToCurl(requestObject));
// Example 3: URL object support (since v0.6.0)
const urlObj = new URL('https://example.com/search');
urlObj.searchParams.set('q', 'test');
urlObj.searchParams.set('page', '2');
const optionsWithUrlObj = { method: 'GET' };
console.log('\n--- cURL from URL object ---');
console.log(fetchToCurl(urlObj, optionsWithUrlObj));