Node libcurl An Ultimate Guide to Mastering Curl Requests in Node.js

Introduction to node-libcurl

Node-libcurl is a powerful library that allows you to perform HTTP requests with ease in Node.js. Leveraging the capabilities of libcurl, it provides a comprehensive set of APIs for various types of requests and communication over different protocols.

Basic HTTP GET Request

Let’s start with a simple HTTP GET request to fetch data from a public API.


const {Curl} = require('node-libcurl');

const curl = new Curl();
curl.setOpt('URL', 'https://jsonplaceholder.typicode.com/posts/1');

curl.on('end', function (statusCode, body, headers) {
    console.info('Status Code: ', statusCode);
    console.info('Response Body: ', body);
    console.info('Response Headers: ', headers);

    this.close();
});

curl.on('error', curl.close.bind(curl));
curl.perform();

HTTP POST Request

To send data to a server, you can use the POST method.


const {Curl} = require('node-libcurl');

const curl = new Curl();
curl.setOpt(Curl.option.URL, 'https://jsonplaceholder.typicode.com/posts');
curl.setOpt(Curl.option.POSTFIELDS, JSON.stringify({
    title: 'foo',
    body: 'bar',
    userId: 1
}));

curl.on('end', function (statusCode, body, headers) {
    console.info('Status Code: ', statusCode);
    console.info('Response Body: ', body);
    console.info('Response Headers: ', headers);

    this.close();
});

curl.on('error', curl.close.bind(curl));
curl.perform();

Handling File Uploads

Uploading files using node-libcurl is straightforward.


const {Curl} = require('node-libcurl');
const fs = require('fs');

const file = fs.readFileSync('path/to/file.jpg');

const curl = new Curl();
curl.setOpt(Curl.option.URL, 'https://example.com/upload');
curl.setOpt(Curl.option.HTTPPOST, [
    {name: 'file', file: 'path/to/file.jpg', type: 'image/jpeg'}
]);

curl.on('end', function (statusCode, body, headers) {
    console.info('Status Code: ', statusCode);
    console.info('Response Body: ', body);
    console.info('Response Headers: ', headers);

    this.close();
});

curl.on('error', curl.close.bind(curl));
curl.perform();

Setting Custom Headers

Custom headers can be added to each request for more control.


const {Curl} = require('node-libcurl');

const curl = new Curl();
curl.setOpt(Curl.option.URL, 'https://example.com');
curl.setOpt(Curl.option.HTTPHEADER, [
    'User-Agent: node-libcurl/1.0',
    'Accept: application/json'
]);

curl.on('end', function (statusCode, body, headers) {
    console.info('Status Code: ', statusCode);
    console.info('Response Body: ', body);
    console.info('Response Headers: ', headers);

    this.close();
});

curl.on('error', curl.close.bind(curl));
curl.perform();

Using Proxies

Proxy configurations can be managed easily with node-libcurl.


const {Curl} = require('node-libcurl');

const curl = new Curl();
curl.setOpt(Curl.option.URL, 'https://example.com');
curl.setOpt(Curl.option.PROXY, 'http://proxyserver:8080');

curl.on('end', function (statusCode, body, headers) {
    console.info('Status Code: ', statusCode);
    console.info('Response Body: ', body);
    console.info('Response Headers: ', headers);

    this.close();
});

curl.on('error', curl.close.bind(curl));
curl.perform();

Full Application Example

Below is a full example that demonstrates fetching a list of posts, creating a new post, and displaying the results on a simple server.


const express = require('express');
const {Curl} = require('node-libcurl');

const app = express();
app.use(express.json());

app.get('/posts', (req, res) => {
    const curl = new Curl();
    curl.setOpt('URL', 'https://jsonplaceholder.typicode.com/posts');

    curl.on('end', function (statusCode, body) {
        res.status(statusCode).send(JSON.parse(body));
        this.close();
    });

    curl.on('error', (error) => {
        res.status(500).send(error.message);
        curl.close();
    });

    curl.perform();
});

app.post('/posts', (req, res) => {
    const curl = new Curl();
    curl.setOpt(Curl.option.URL, 'https://jsonplaceholder.typicode.com/posts');
    curl.setOpt(Curl.option.POSTFIELDS, JSON.stringify(req.body));

    curl.on('end', function (statusCode, body) {
        res.status(statusCode).send(JSON.parse(body));
        this.close();
    });

    curl.on('error', (error) => {
        res.status(500).send(error.message);
        curl.close();
    });

    curl.perform();
});

app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

Hash: c19d105d1d3538fcd2cfb73b9f8feed80c089f431d3518a2e7a0fcfc6c9a723e

Leave a Reply

Your email address will not be published. Required fields are marked *