Debug API calls by printing human-readable cURL strings
- cURL is the most well-known command line tool for API calls
- Logging cURL commands to the console helps debug API calls
- cURL commands are easy to read (when printed in human readable format)
- cURL commands can be pasted into a terminal or Postman
import curlString from 'curl-string';
const debug = true;
const url = 'http://example.com';
const options = {
method: 'post',
headers: { accept: 'application/json', 'accept-language': 'en' },
body: { hello: 'world' }
};
if (debug) {
console.log(curlString(url, options));
}
// make real call here, e.g. fetch(url, options);
curlString
supports an optional 3rd parameter config object:
const curlStringOptions = { colorJson: true, jsonIndentWidth: 2}; // (these are the defaults)
const str = curlString(url, options, curlStringOptions);
Converted to ESM (import
instead of require
).
Use this version if you still need require
syntax (CommonJS).