How to use cURL with JavaScript and NodeJS

How to use cURL with JavaScript and NodeJS

Are you here searching for some useful tutorials on using cURL with Javascript and Node JS? You have landed at the right place. 

This ultimate guide will demonstrate using cURL with Javascript and Node JS. We will cover various aspects of cURL, including Client-Side and Server-Side cURL in Javascript. 

Sound alluring? Let’s jump in. 😀

Introduction

cURL is a well-known command-line utility for transmitting data over multiple protocols such as HTTP, FTP, and SFTP. It is frequently used in web development to automate web scraping operations, test APIs, and retrieve files.

This tutorial will look at using cURL with JavaScript and Node.js. We’ll talk about the following topics:

  • What is cURL in JavaScript?
  • Is cURL in JavaScript Useful for Data Scraping?
  • Client-side cURL in JavaScript and Node Js 
  • Server-side cURL in JavaScript and Node Js
  • Connect cURL to Proxy Server

What is cURL in JavaScript?

cURL is not a part of JavaScript, rather it is a command-line tool and library that may be used to make HTTP queries.

Although there is no built-in method for making HTTP requests from within JavaScript, several libraries and tools, including Axios, fetch, etc., may be used to accomplish so.

Data scraping is one of the cURL’s main applications. Data scraping is the process of retrieving information from websites and saving it in a structured format, such as CSV or JSON. cURL can send HTTP queries to websites and obtain HTML from web pages. Afterward, relevant data can be extracted from this text by parsing it.

Although JavaScript does not natively support cURL, it can be used with Node.js using packages like `node-cURL` or `node-libcURL`. With the help of these packages, a Node.js application can issue HTTP requests while using cURL.

In addition to using cURL with Node.js, cURL instructions can be translated to JavaScript code using tools such as `cURLconverter` or` cURL-to-js`. With the help of these tools, it is simple to translate cURL commands into JavaScript code for usage in client- or server-side applications.

Is cURL in JavaScript Useful for Data Scraping?

Using cURL with Node.js can be beneficial for data scraping tasks because it offers a more effective and scalable solution than client-side JavaScript.

The client-side Javascript used for data scraping may cause performance issues and limit the number of requests because all queries are sent through the user’s browser. However, Node.js allows requests to be sent from a server or cloud-based platform, which improves the effectiveness and scalability of data scraping.

cURL can be used in web development and scraping tasks, including user authentication, proxy support, file transfer, SSL connections, and more.

User authentication: 

cURL can be used to authenticate users by providing their necessary credentials along with HTTP requests to login sites. This is often used in web scraping operations where the data is hidden behind a login page.

Proxy Support: 

Using proxies, cURL can send requests from a different IP address or get around network restrictions. This can be helpful during web scraping since the website may block some IP addresses.

File transfer: 

You can upload and download files from servers using cURL. This is helpful in web development for downloading files from a remote site or uploading files to a server.

SSL connections: 

HTTPS requests can be made securely using cURL, which supports SSL encryption. This is significant for web development tasks that handle sensitive data, such as passwords or financial data.

Testing APIs: 

API testing is frequently done using cURL, which sends HTTP requests to endpoints and checks the results. Building RESTful APIs or integrating with third-party APIs are two activities in web development where this might be helpful.

Web scraping: 

By sending queries to websites and requesting the HTML content, cURL can be used for web scraping activities. The extracted data can be presented in various formats like CSV, JSON, etc. 

How to Use cURL in JavaScript and Node JS?

cURL can be used with Node.js in JavaScript using packages like `node-cURL`  or `node-libcURL`. These packages offer means for sending cURL-based HTTP requests from a Node.js application. This section will go through the steps to use cURL in JavaScript and Node.js.

Using cURL with JavaScript:

  1. Open the command-line window of your system.
  2. Enter the cURL command, then the URL from which you want to retrieve data. For instance: `cURL https://httpbin.org/`
  3. After you press enter key, the command will send an HTTP request and get the HTML content of the website.

Using cURL with Node.js:

The first step is to install the required package. This tutorial uses the `node-fetch` package. You can install this package using the following npm command:

npm install node-fetch@2

When installed successfully, import the node-cURL package in your JavaScript file like this:

const fetch = require('node-fetch');

In the next step, we will pass the URL to the `fetch()` function. The response is received by the `then()` function, which logs it on the screen.

fetch('http://quotes.toscrape.com')
  .then(response => response.text())
  .then(body => console.log(body))
  .catch(err => console.error(err));

The complete code is as follows:

const fetch = require('node-fetch');
fetch('http://quotes.toscrape.com')
  .then(response => response.text())
  .then(body => console.log(body))
  .catch(err => console.error(err));

Client-side cURL in JavaScript and Node Js – Alternatives and Solutions

Client-side cURL does not exist in JavaScript. In web development, it is more typical to make HTTP queries from within a web browser using client-side HTTP libraries such as Axios, fetch, or XMLHttpRequest.

Although these libraries offer similar functionality to cURL, they are made to run in the browser and make and receive HTTP requests and responses using XMLHttpRequest or the Fetch API.

The XMLHttpRequest (XHR) object is one of JavaScript’s most often used substitutes for the cURL function. With this object, you can send asynchronous calls to web servers and get responses in plain text, XML, or JSON. Another well-liked option is the fetch API, a more recent and straightforward method of sending HTTP requests using JavaScript.

Making a GET request using XHR:

To make a GET request using XHR, you can use the `open()` function and specify “GET” as the first parameter.

var XMLHttpRequest = require('xhr2');
const xhrhttp = new XMLHttpRequest(); 
xhrhttp.open('GET', 'http://quotes.toscrape.com'); 
xhrhttp.onreadystatechange = function() { 
              if (xhrhttp.readyState === XMLHttpRequest.DONE) {   
                            console.log(xhrhttp.responseText); 
               } 
}; 
xhrhttp.send();

The output above shows the HTML output of the quotes website.

Making a GET request using fetch:

Using the fetch package, we can use the fetch constructor and pass the URL to get the HTML output.

fetch(‘http://quotes.toscrape.com’)
  .then(response => response.json())
  .then(data => console.log(data));

Sending headers using XHR:

We can set the headers for our request using the `setRequestHeader()` function.

var XMLHttpRequest = require('xhr2');
const xhrhttp = new XMLHttpRequest();
xhrhttp.open('GET', 'http://quotes.toscrape.com');
xhrhttp.setRequestHeader('Content-Type', 'application/json');
xhrhttp.onreadystatechange = function() {
  if (xhrhttp.readyState === XMLHttpRequest.DONE) {
    console.log(xhrhttp.responseText);
  }
};
xhrhttp.send();

Sending headers using fetch:

Similarly, we can set the headers using Fetch API as well as shown in the code snippet below.

fetch(''http://quotes.toscrape.com'', {
  headers: {
    'Content-Type': 'application/json'
  }
})
  .then(response => response.json())
  .then(data => console.log(data));

Sending cookies using XHR:

Cookies are frequently used for session management and authentication in the context of XMLHttpRequest (XHR). In order to authenticate the user and obtain data from the server, the website may need to include the session cookie in its XHR requests if the user logs into the website and the website sets a session cookie in the user’s browser.

Without the required cookies, the server might not identify the user as authenticated and might reject the request or provide inaccurate data. To guarantee that the website functions properly, it is essential that it provides the appropriate cookies along with XHR requests.

To set the cookies with your request, you first need to get `withCredentials` as true and then set the required cookie. In the example below, we are setting the `session_id` cookie value.

var XMLHttpRequest = require('xhr2');
const xhrhttp = new XMLHttpRequest();
xhrhttp.open('GET', ''http://quotes.toscrape.com'');
xhrhttp.withCredentials = true;
xhrhttp.setRequestHeader('Cookie', 'session_id=12345');
xhrhttp.onreadystatechange = function() {
  if (xhrhttp.readyState === XMLHttpRequest.DONE) {
    console.log(xhrhttp.responseText);
  }
};
xhrhttp.send();

Sending cookies using fetch:

Fetch API can also send the cookies by setting the `credentials` parameter to the value `include`. This will tell the API to include all the cookies that are set by the browser. Since, session_id is already set in the users’ browsers, it will be included in the request.

fetch(''http://quotes.toscrape.com'', { 
  credentials: 'include'
})
  .then(response => response.json())
  .then(data => console.log(data));

Getting JSON using XHR:

There are some websites that return the data in JSON format. We can parse that JSON data using XMLHttpRequest. For this example, we will send the request to the JSON Test website and get the current date in JSON format.

var XMLHttpRequest = require('xhr2');
const xhrhttp = new XMLHttpRequest();
xhrhttp.open('GET', ' http://date.jsontest.com');
xhrhttp.setRequestHeader('Accept', 'application/json');
xhrhttp.onreadystatechange = function() {
  if (xhrhttp.readyState === XMLHttpRequest.DONE) {
    console.log(JSON.parse(xhrhttp.responseText));
  }
};
xhrhttp.send();

This will give the following output:

Getting JSON using fetch:

Similarly, we can also get the JSON data using the Fetch API in the following way:

fetch('http://date.jsontest.com', {
  headers: {
    'Accept': 'application/json'
  }
})
  .then(response => response.json())
  .then(data => console.log(data));

Server-side cURL in JavaScript and Node Js

When used on the server side, cURL can perform a range of activities including sending HTTP requests with specified headers, processing cookies, managing SSL/TLS connections, and supporting various authentication methods. This makes it an effective tool for automating chores and web development.

The built-in`http` and `https` modules of Node.js and third-party libraries like `node-fetch` and `node-libcurl` can all be used to implement cURL in a server-side environment. With the help of these tools, developers may quickly send HTTP queries and interact with web services from within their server-side JavaScript code.

The `node-fetch` is a Node.js package that provides a `fetch` API that can be used to perform HTTP queries. It offers a modern, user-friendly interface for sending HTTP requests and is comparable to the `fetch` API in web browsers.

Node-fetch may implement server-side cURL by providing a similar interface to cURL, but instead of command-line arguments, it uses JavaScript code. Here is an example of how to use node-fetch to send a GET request to a URL:

const fetch = require('node-fetch');

fetch(''http://quotes.toscrape.com'')
  .then(response => response.text())
  .then(body => console.log(body))
  .catch(err => console.error(err));

In this example, a GET request is made to https://example.com using `fetch`. The response is handled by `then`, and the response body is logged to the console. Moreover, `catch` is used to deal with potential exceptions.

The above code will return the HTML response of the mentioned website and displays it on the screen like this:

Node-fetch can also help in making complex HTTP requests, such as POST requests that require headers and data:

const fetch = require('node-fetch');

fetch('http://quotes.toscrape.com/login', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  body: 'username=myusername&password=mypassword'
})
.then(response => {
  console.log(response);
})
.catch(error => {
  console.error(error);
});

In this example, a POST request is sent to http://quotes.toscrape.com/login using node-fetch. We chose `application/x-www-form-urlencoded` as the default content type for HTML form data by setting the Content-Type header. We also set a URL-encoded string containing the login and password values as the request’s body.

Executing this code will give the following output:

Connect cURL to Proxy Server

Proxy servers are an excellent choice for web scraping because they give an extra layer of anonymity and help users overcome IP bans and other limitations that may be applied to a particular IP address. The website you are scraping sees the proxy server’s IP address when you send queries rather than your IP address. This can help prevent the website you’re scraping from blocklisting or rate-limiting your IP address.

If you are using the `node-fetch` library to make HTTP requests in Node.js, you can connect to a proxy server using the `HttpAgent` or `HttpsAgent` classes. Here’s an example of using a proxy server that requires authentication using the `node-fetch`:

const fetch = require('node-fetch');
const HttpsProxyAgent = require('https-proxy-agent');

const url = 'https://example.com';
const proxyUrl = 'http://proxy.example.com:8080';
const username = 'myusername';
const password = 'mypassword';

const agent = new HttpsProxyAgent({
  host: 'proxy.example.com',
  port: '8080',
  auth: `${username}:${password}`
});

fetch(url, { agent })
  .then(response => response.text())
  .then(data => console.log(data))
  .catch(error => console.error(error));

In this example, the `host`, `port`, and `auth` options are used to create a new HttpsPoxyAgent. The `username` and` password` are concatenated into a string and entered as the auth option, which is set to `$username:$password`.

The `agent` and `URL` we wish to get are then sent to the `fetch` method. The `agent` option tells node-fetch to use the `HttpsProxyAgent` for the request. This should enable you to use node-fetch to send requests through a proxy server that needs authentication.

Conclusion

A well-liked and efficient method for extracting data from websites is to use cURL in conjunction with JavaScript and NodeJS. While using cURL for online scraping, there are a few quirks to consider, such as the requirement for proxy servers to prevent being recognized and blocked by websites.

XMLHttpRequest, fetch, and Axios are several replacements to cURL that can be used for client-side requests even though cURL itself is not supported in client-side JavaScript. NodeJS offers several choices for implementing cURL requests on the server, including libraries like `node-fetch` and `node-libcurl`.

In conclusion, cURL combined with JavaScript and NodeJS is a strong and adaptable web scraping solution that can help in automating the process of obtaining data from websites.

Frequently Asked Questions (FAQs)

What is a cURL Request?

A cURL request is a command-line tool used to send or receive data to or from a server using one of the supported protocols (HTTP, FTP, SMTP, etc.). A cURL request typically consists of a URL pointing to an online resource and several customizable options, including headers, data payloads, and login credentials.

How do I send cURL requests as a javascript fetch request?

To send a cURL request as a JavaScript Fetch request, you must translate the cURL options into corresponding Fetch API options. The Fetch API, a web API, offers an up-to-date interface for retrieving resources from the internet.

An illustration of how to change a cURL request into a Fetch request is shown below:

cURL Request: cURL https://httpbin.org/

The corresponding request using Fetch API will be:

fetch(‘ https://httpbin.org/’)

  .then(response => response.json())

  .then(data => console.log(data))

  .catch(error => console.error(error));

How do I scrape a website with cURL?

  1. Determine the website’s URL that you want to scrape.
  2. Open a terminal program or the command-line interface (CLI).
  3. Enter the website URL you want to scrape, followed by the cURL command. For instance: curl https://httpbin.org
  4. Provide the necessary headers or authentication information in your cURL command if the website asks for them. For example: curl -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" https://httpbin.org
  5. Use the -o or —output option followed by the desired filename to save the scraped data to a file. For example: curl https://httpbin.org -o output.html
  6. Examine the data you scraped to get the information you’re looking for. Depending on your requirements and the data structure, you can use various tools and strategies to do this.

Leave a Comment