node-libcurl

The fastest feature-rich URL transfer library for Node.js.
libcurl bindings for Node.js. libcurl official description:
libcurl is a free and easy-to-use client-side URL transfer library, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet and TFTP. libcurl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, Kerberos), file transfer resume, http proxy tunneling and more!
Quick Start
Note:
- This library cannot be used in a browser, it depends on native code.
Install
npm i node-libcurl --save
or
pnpm i node-libcurl --save
or
yarn add node-libcurl
Simple Request - Async / Await using curly
this API is experimental and is subject to changes without a major version bump
const { curly } = require('node-libcurl');
const { statusCode, data, headers } = await curly.get('https://www.google.com')
Any option can be passed using their FULLNAME or a lowerPascalCase format:
const querystring = require('querystring');
const { curly } = require('node-libcurl');
const { statusCode, data, headers } = await curly.post('https://httpbin.com/post', {
postFields: querystring.stringify({
field: 'value',
}),
// can use `postFields` or `POSTFIELDS`
})
JSON POST example:
const { curly } = require('node-libcurl')
const { data } = await curly.post('https://httpbin.com/post', {
postFields: JSON.stringify({ field: 'value' }),
httpHeader: [
'Content-Type: application/json',
'Accept: application/json'
],
})
console.log(data)
Simple Request - Using Curl class
const { Curl } = require('node-libcurl');
const curl = new Curl();
curl.setOpt('URL', 'www.google.com');
curl.setOpt('FOLLOWLOCATION', true);
curl.on('end', function (statusCode, data, headers) {
console.info(statusCode);
console.info('---');
console.info(data.length);
console.info('---');
console.info(this.getInfo( 'TOTAL_TIME'));
this.close();
});
curl.on('error', curl.close.bind(curl));
curl.perform();
Setting HTTP headers
Pass an array of strings specifying headers
curl.setOpt(Curl.option.HTTPHEADER,
['Content-Type: application/x-amz-json-1.1'])
Form Submission (Content-Type: application/x-www-form-urlencoded)
const querystring = require('querystring');
const { Curl } = require('node-libcurl');
const curl = new Curl();
const close = curl.close.bind(curl);
curl.setOpt(Curl.option.URL, '127.0.0.1/upload');
curl.setOpt(Curl.option.POST, true)
curl.setOpt(Curl.option.POSTFIELDS, querystring.stringify({
field: 'value',
}));
curl.on('end', close);
curl.on('error', close);
MultiPart Upload / HttpPost libcurl Option (Content-Type: multipart/form-data)
const { Curl } = require('node-libcurl');
const curl = new Curl();
const close = curl.close.bind(curl);
curl.setOpt(Curl.option.URL, '127.0.0.1/upload.php');
curl.setOpt(Curl.option.HTTPPOST, [
{ name: 'input-name', file: '/file/path', type: 'text/html' },
{ name: 'input-name2', contents: 'field-contents' }
]);
curl.on('end', close);
curl.on('error', close);
Binary Data
When requesting binary data make sure to do one of these:
curl.setOpt('WRITEFUNCTION', (buffer, size, nmemb) => {
// something
})
- Enable one of the following flags:
curl.enable(CurlFeature.NoDataParsing)
// or
curl.enable(CurlFeature.Raw)
The reasoning behind this is that by default, the Curl instance will try to decode the received data and headers to utf8 strings, as can be seen here: https://github.com/JCMais/node-libcurl/blob/b55b13529c9d11fdcdd7959137d8030b39427800/lib/Curl.ts#L391
For more examples check the examples folder.
SSL
API
API documentation for the latest stable version is available at https://node-libcurl-docs.netlify.app/modules/lib_index.html.
Develop branch documentation is available at https://develop--node-libcurl-docs.netlify.app/modules/lib_index.html.
This library provides Typescript type definitions.
Almost all CURL options are supported, if you pass one that is not, an error will be thrown.
For more usage examples check the examples folder.
Special Notes
READFUNCTION option
The buffer passed as first parameter to the callback set with the READFUNCTION option is initialized with the size libcurl is using in their upload buffer (which can be set with UPLOAD_BUFFERSIZE), this is initialized using node::Buffer::Data(buf); which is basically the same than Buffer#allocUnsafe and therefore, it has all the implications as to its correct usage: https://nodejs.org/pt-br/docs/guides/buffer-constructor-deprecation/#regarding-buffer-allocunsafe
So, be careful, make sure to return exactly the amount of data you have written to the buffer on this callback. Only that specific amount is going to be copied and handed over to libcurl.
Common Issues
See COMMON_ISSUES.md
Benchmarks
See ./benchmark
Security
See SECURITY.md
Supported Libcurl Versions
The addon is only tested against libcurl version 7.81.0 and the latest one available.
The code itself is only made to compile with versions greater than or equal to 7.81.0, any libcurl version lower than that is not supported.
The 7.81.0 version was released on Jan 5 2022, and it is the version shipped with Ubuntu 22.04. There has been more than 5541 bug fixes on libcurl since then.
For Enterprise
node-libcurl is available as part of the Tidelift Subscription.
The maintainers of node-libcurl and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.
Detailed Installation
The latest version of this package has prebuilt binaries (thanks to node-pre-gyp)
available for:
And on the following platforms:
- Linux 64 bits & ARM64 & Alpine (musl, 64 bits)
- macOS 64 bits (Intel) & ARM64 (M1+)
- Windows 64 bits
Installing with yarn add node-libcurl or npm install node-libcurl should download a prebuilt binary and no compilation will be needed. However if you are trying to install on nw.js or electron additional steps will be required, check their corresponding section below.
The prebuilt binary is statically built with the following library versions, features and protocols (library versions may change between Node.js versions):
Version: libcurl/8.17.0 OpenSSL/3.5.2 zlib/1.3.1 brotli/1.1.0 zstd/1.5.7 libidn2/2.1.1 libssh2/1.10.0 nghttp2/1.66.0 ngtcp2/1.17.0 nghttp3/1.12.0 OpenLDAP/2.6.9
Protocols: dict, file, ftp, ftps, gopher, gophers, http, https, imap, imaps, ldap, ldaps, mqtt, pop3, pop3s, rtsp, scp, sftp, smb, smbs, smtp, smtps, telnet, tftp, ws, wss
Features: AsynchDNS, IDN, IPv6, Largefile, NTLM, SSL, libz, brotli, TLS-SRP, HTTP2, UnixSockets, HTTPS-proxy, alt-svc
If there is no prebuilt binary available that matches your system, or if the installation fails, then you will need an environment capable of compiling Node.js addons, which means:
- python 3.x installed
- updated C++ compiler able to compile C++17 (C++20 for Electron >= v32).
If you don't want to use the prebuilt binary even if it works on your system, you can pass a flag when installing:
With npm
npm install node-libcurl --build-from-source
With yarn
npm_config_build_from_source=true yarn add node-libcurl
Important Notes on Prebuilt Binaries / Direct Installation
Those notes are not important when building on Windows
The prebuilt binaries are statically linked with brotli, libidn2, libssh2, openLDAP, OpenSSL nghttp2, zstd and zlib.
The brotli, nghttp2, OpenSSL and zlib versions must match the version Node.js uses, this is necessary to avoid any possible issues by mixing library symbols of different versions, since Node.js also exports some of the symbols of their deps.