Axios As Curl
A Node.js library that provides an Axios-like interface while using curl
under the hood. Perfect for scenarios where you need the power of curl with the convenience of Axios's API.
But Why?
Axios and other NodeJS request clients are notorious for "ECONNREFUSED" and "ERR_NETWORK" errors. While there are workarounds too keep the agents alive and so on, they also break from time to time. I therefore developed this in-place-replacement for Axios which uses the more powerful curl
to actully run the requests. It solved my problems immediately and I hope it solves yours too! ♥
Features
- 🔄 Axios-compatible API
- 🛠️ Powered by system curl
- 📦 Support for multiple response types (JSON, text, stream, buffer)
- 📊 Detailed request metadata and timing information
- 🔁 Automatic retries with exponential backoff
- 📝 FormData support
- 🔍 Redirect following
- 🗑️ Automatic temporary file cleanup
Installation
npm install axios-as-curl
Basic Usage
import AxiosAsCurl from 'axios-as-curl';
const client = new AxiosAsCurl();
const response = await client.get('https://api.example.com/data');
console.log(response.data);
Configuration
Default Configuration
const client = new AxiosAsCurl({
timeout: 10000,
maxRetries: 3,
responseType: 'json',
headers: {
'User-Agent': 'Custom User Agent',
Accept: '*/*',
},
});
Available Response Types
json
(default): Parses response as JSONtext
: Returns raw text responsestream
: Returns a readable streambuffer
: Returns response as Buffer
Examples
POST Request with JSON Data
const response = await client.post('https://api.example.com/users', {
name: 'John Doe',
email: 'john@example.com',
});
console.log(response.data);
console.log(response.metadata.duration);
File Upload with FormData
import FormData from 'form-data';
const form = new FormData();
form.append('file', Buffer.from('Hello World'), 'hello.txt');
form.append('name', 'test-file');
const response = await client.post('https://api.example.com/upload', form);
Streaming Response
const response = await client.get('https://api.example.com/download', {
responseType: 'stream',
});
import { createWriteStream } from 'fs';
response.data.pipe(createWriteStream('download.file'));
Request Metadata
Each response includes detailed metadata:
const response = await client.get('https://api.example.com/data');
console.log(response.metadata);
API Reference
Constructor Options
interface AxiosAsCurlConfig {
timeout?: number;
maxRetries?: number;
responseType?: 'json' | 'text' | 'stream' | 'buffer';
headers?: Record<string, string>;
}
Available Methods
All methods return a Promise with response object:
get(url, config?)
post(url, data?, config?)
put(url, data?, config?)
patch(url, data?, config?)
delete(url, config?)
request(config)
Response Object
interface AxiosAsCurlResponse {
data: any;
status: number;
statusText: string;
headers: Record<string, string>;
config: AxiosAsCurlConfig;
metadata: {
startTime: number;
endTime: number;
duration: number;
retries: number;
redirects: number;
tempFiles: number;
finalUrl: string;
timings: {
dns: number;
connect: number;
ttfb: number;
total: number;
};
};
}
Error Handling
try {
const response = await client.get('https://api.example.com/data');
} catch (error) {
console.error(`Request failed: ${error.message}`);
}
Common Use Cases
const response = await client.get('https://api.example.com/data', {
headers: {
Authorization: 'Bearer token',
'Custom-Header': 'value',
},
});
Retry Configuration
const client = new AxiosAsCurl({
maxRetries: 5,
});
Large File Download
const response = await client.get('https://api.example.com/large-file', {
responseType: 'stream',
});
await pipeline(response.data, createWriteStream('large-file.zip'));
Notes
- Requires
curl
to be installed on the system - Temporary files are automatically cleaned up after each request
- Follows redirects by default
- Uses exponential backoff for retries
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.