curl2axios is a lightweight Axios interceptor for generating cURL commands from Axios requests. It simplifies the process of converting your Axios requests into cURL commands for easier debugging and sharing.
Install the library using npm or yarn:
npm install curl2axios
OR
yarn add curl2axios
Basic Usage
import axios from 'axios';
import { curlInterceptor } from 'curl2axios';
axios.interceptors.request.use(curlInterceptor);
axios.get('https://api.com/item/1');
This will automatically generate a cURL command in the console based on the Axios request.
{
occurredAt: '2024-02-02T22:17:45.514Z',
curlCommand: "curl 'https://api.com/item/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: '"
}
Custom Callback You can also provide a custom callback function to handle the generated cURL command. This is useful if you want to do something specific with the command, such as logging it in a different way.
import axios from 'axios';
import { curlInterceptor } from 'curl2axios';
const myCallback = (curl: string) => console.log(curl);
axios.interceptors.request.use((req) => curlInterceptor(req, myCallback));
axios.get('https://api.com/item/1');
In this example, the custom callback function myCallback will be called with the generated cURL command.
For the given Axios request:
axios.get('https://api.com/item/1');
// curl 'https://api.com/item/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: '
import axios from 'axios';
import { bindCurl } from 'curl2axios';
const instance = bindCurl(axios.create({ baseURL: 'https://pokeapi.co/api/v2' }));
await instance.get('/berry-flavor/1');
console.log(instance.curl);
// > "curl '/berry-flavor/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: ' --url 'https://pokeapi.co/api/v2'"
axios2curl is licensed under the MIT License - see the LICENSE file for details.