Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x, 18.x]
node-version: [24.x, 22.x]
fail-fast: true

steps:
Expand Down
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
node_modules
dist/**/*
coverage
node_modules/
dist/
coverage/

.vscode/

149 changes: 58 additions & 91 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

[![NPM Version](https://img.shields.io/npm/v/sync-request-curl?logo=npm)](https://www.npmjs.com/package/sync-request-curl?activeTab=versions)
 
[![install size](https://packagephobia.com/badge?p=sync-request-curl)](https://packagephobia.com/result?p=sync-request-curl)
 
[![Depfu Dependencies](https://badges.depfu.com/badges/6c4074c4d23ad57ee2bfd9ff90456090/overview.svg)](https://depfu.com/github/nktnet1/sync-request-curl?project_id=39032)
 
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fnktnet1%2Fsync-request-curl.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fnktnet1%2Fsync-request-curl?ref=badge_shield)
Expand All @@ -30,20 +28,8 @@
 
[![DeepSource](https://app.deepsource.com/gh/nktnet1/sync-request-curl.svg/?label=active+issues&show_trend=true&token=OTP6tE2be4X1kvxZRsxRh25e)](https://app.deepsource.com/gh/nktnet1/sync-request-curl/)
 
[![codebeat badge](https://codebeat.co/badges/8bdb4562-0492-4c1c-8b02-e69c94373d60)](https://codebeat.co/projects/github-com-nktnet1-sync-request-curl-main)
 
[![GitHub stars](https://img.shields.io/github/stars/nktnet1/sync-request-curl.svg?style=social)](https://github.com/nktnet1/sync-request-curl/stargazers)

[![Downloads Total](https://badgen.net/npm/dt/sync-request-curl)](https://moiva.io/?npm=sync-request-curl)
 
[![Downloads Yearly](https://badgen.net/npm/dy/sync-request-curl)](https://moiva.io/?npm=sync-request-curl)
 
[![Downloads Monthly](https://badgen.net/npm/dm/sync-request-curl)](https://moiva.io/?npm=sync-request-curl)
 
[![Downloads Weekly](https://badgen.net/npm/dw/sync-request-curl)](https://moiva.io/?npm=sync-request-curl)
 
[![Downloads Daily](https://badgen.net/npm/dd/sync-request-curl)](https://moiva.io/?npm=sync-request-curl)

---

Make synchronous web requests similar to [sync-request](https://github.com/ForbesLindesay/sync-request), but up to 20 times more quickly.
Expand All @@ -60,16 +46,16 @@ Designed to run on NodeJS. It will not work in a browser.

- [1. Installation](#1-installation)
- [2. Usage](#2-usage)
- [2.1. Method](#21-method)
- [2.2. URL](#22-url)
- [2.3. Options](#23-options)
- [2.4. Response](#24-response)
- [2.5. Errors](#25-Errors)
- [2.1. Method](#21-method)
- [2.2. URL](#22-url)
- [2.3. Options](#23-options)
- [2.4. Response](#24-response)
- [2.5. Errors](#25-Errors)
- [3. License](#3-license)
- [4. Compatibility](#4-compatibility)
- [4.1. Windows](#41-windows)
- [4.2. MacOS](#42-macos)
- [4.3. Linux](#43-linux)
- [4.1. Windows](#41-windows)
- [4.2. MacOS](#42-macos)
- [4.3. Linux](#43-linux)
- [5. Caveats](#5-caveats)

## 1. Installation
Expand Down Expand Up @@ -101,10 +87,7 @@ request(method, url, options);
```typescript
import request from 'sync-request-curl';

const res = request(
'GET',
'https://comp1531namesages.alwaysdata.net'
);
const res = request('GET', 'https://comp1531namesages.alwaysdata.net');
console.log('Status Code:', res.statusCode);
const jsonBody = JSON.parse(res.body.toString());
console.log('Returned JSON object:', jsonBody);
Expand All @@ -115,13 +98,9 @@ console.log('Returned JSON object:', jsonBody);
```typescript
import request from 'sync-request-curl';

const res = request(
'GET',
'https://comp1531forum.alwaysdata.net/echo/echo',
{
qs: { message: 'Hello, world!' },
}
);
const res = request('GET', 'https://comp1531forum.alwaysdata.net/echo/echo', {
qs: { message: 'Hello, world!' },
});
console.log('Status Code:', res.statusCode);
const jsonBody = JSON.parse(res.body.toString());
console.log('Returned JSON object:', jsonBody);
Expand All @@ -132,17 +111,13 @@ console.log('Returned JSON object:', jsonBody);
```typescript
import request from 'sync-request-curl';

const res = request(
'POST',
'https://comp1531quiz.alwaysdata.net/quiz/create',
{
headers: { lab08quizsecret: "bruno's fight club" },
json: {
quizTitle: 'New Quiz',
quizSynopsis: 'Sync request curl example'
},
}
);
const res = request('POST', 'https://comp1531quiz.alwaysdata.net/quiz/create', {
headers: { lab08quizsecret: "bruno's fight club" },
json: {
quizTitle: 'New Quiz',
quizSynopsis: 'Sync request curl example',
},
});

console.log('Status Code:', res.statusCode);
const jsonBody = JSON.parse(res.body.toString());
Expand All @@ -154,41 +129,34 @@ console.log('Returned JSON Object:', jsonBody);
```typescript
import request from 'sync-request-curl';

const res = request(
'POST',
'https://example.com/upload',
{
formData: [
{
name: 'example-file',
file: './path/to/file.txt',
type: 'text/plain',
},
{
name: 'example-content',
contents: 'Example Content!',
},
],
}
);
const res = request('POST', 'https://example.com/upload', {
formData: [
{
name: 'example-file',
file: './path/to/file.txt',
type: 'text/plain',
},
{
name: 'example-content',
contents: 'Example Content!',
},
],
});

console.log('Status Code:', res.statusCode);
```

Using a proxy URL (Note: replace with your own proxy details)

```javascript
import request from 'sync-request-curl';

const res = request(
'GET',
'https://ipinfo.io/json',
{
setEasyOptions: (curl, options) => {
curl.setOpt(options.PROXY, 'http://your-proxy-url:port');
curl.setOpt(options.PROXYUSERPWD, 'proxyUsername:proxyPassword');
},
}
);
const res = request('GET', 'https://ipinfo.io/json', {
setEasyOptions: (curl, options) => {
curl.setOpt(options.PROXY, 'http://your-proxy-url:port');
curl.setOpt(options.PROXYUSERPWD, 'proxyUsername:proxyPassword');
},
});

console.log('Status Code:', res.statusCode);
const jsonBody = JSON.parse(res.body.toString());
Expand All @@ -202,13 +170,15 @@ console.log(jsonBody);
### 2.1. Method

HTTP method (of type `HttpVerb`)

- e.g. `PUT`/`POST`/`GET`/`DELETE`

Note that for the `HEAD` method, [CURLOPT_NOBODY](https://curl.se/libcurl/c/CURLOPT_NOBODY.html) is set to `true` automatically by **sync-request-curl**.

### 2.2. URL

URL as a string

- e.g. https://toohak.fly.dev

### 2.3. Options
Expand All @@ -221,7 +191,6 @@ URL as a string
<th>Default</th>
</tr>


<tr>
<td>qs</td>
<td>
Expand Down Expand Up @@ -251,7 +220,6 @@ URL as a string
<td><code>undefined</code></td>
</tr>


<tr>
<td>json</td>
<td>
Expand All @@ -265,7 +233,6 @@ URL as a string
<td><code>undefined</code></td>
</tr>


<tr>
<td>body</td>
<td>
Expand All @@ -280,7 +247,6 @@ JSON.stringify({
<td><code>undefined</code></td>
</tr>


<tr>
<td>formData</td>
<td>
Expand Down Expand Up @@ -308,8 +274,6 @@ JSON.stringify({
<td><code>0</code><br/>(no timeout)</td>
</tr>



<tr>
<td>followRedirects</td>
<td>
Expand All @@ -319,23 +283,20 @@ JSON.stringify({
<td><code>true</code></td>
</tr>


<tr>
<td>maxRedirects</td>
<td>Sets the maximum number of redirects to follow before throwing an Error.</td>
<td><pre>3</pre></td>
<td><code>-1</code><br/>(no limit)</td>
</tr>


<tr>
<td>insecure</td>
<td> Set to true to send insecure requests. This can be useful on Windows which may have SSL issues (Libcurl code 60).</td>
<td><pre>true</pre></td>
<td><code>false</code></td>
</tr>


<tr>
<td>setEasyOptions</td>
<td>Optional callback to set additional curl options for the <a href='https://curl.se/libcurl/c/easy_setopt_options.html' target="_blank">Easy Interface</a>. This has priority over existing options.</td>
Expand Down Expand Up @@ -398,7 +359,7 @@ export interface Response {
url: string;
body: string | Buffer;
getBody: (encoding?: BufferEncoding) => string | Buffer; // simplified
getJSON: <T = any>(encoding?: BufferEncoding) => T
getJSON: <T = any>(encoding?: BufferEncoding) => T;
}
```

Expand All @@ -407,27 +368,29 @@ export interface Response {
When using the `response.getBody()` function, a generic [Error](https://nodejs.org/api/errors.html) object is thrown.

If there are issues with the request, a `CurlError` will be thrown. This will contain a non-zero `code` property that corresponds to a specific Libcurl Error from this documentation:

- https://curl.se/libcurl/c/libcurl-errors.html

A few common errors are:

1. CURLE_COULDNT_CONNECT (7)
- Failed to connect() to host or proxy.
- **HINT**: This means that the server could not be reached. For local development (e.g. in testing), ensure that your server has started successfully, or that it did not crash while handling a previous request
- Failed to connect() to host or proxy.
- **HINT**: This means that the server could not be reached. For local development (e.g. in testing), ensure that your server has started successfully, or that it did not crash while handling a previous request
1. CURLE_URL_MALFORMAT (3)
- The URL was not properly formatted.
- **HINT**: The request was not successful because your input URL is invalid, e.g. missing domain, protocol or port. Try printing out your input URL, or if it is a GET request, access it directly in a browser
- The URL was not properly formatted.
- **HINT**: The request was not successful because your input URL is invalid, e.g. missing domain, protocol or port. Try printing out your input URL, or if it is a GET request, access it directly in a browser
1. CURLE_PEER_FAILED_VERIFICATION (60)
- The remote server's SSL certificate or SSH fingerprint was deemed not OK. This error code has been unified with CURLE_SSL_CACERT since 7.62.0. Its previous value was 51
- **HINT**: See the [Windows](#41-windows) compatibility section for an explanation and potential workaround
- The remote server's SSL certificate or SSH fingerprint was deemed not OK. This error code has been unified with CURLE_SSL_CACERT since 7.62.0. Its previous value was 51
- **HINT**: See the [Windows](#41-windows) compatibility section for an explanation and potential workaround

It is possible to check the Curl code as follows:

<details closed>
<summary>Example (click to view)</summary>

```typescript
import request, { CurlError } from 'sync-request-curl';
import { CurlError } from 'sync-request-curl/errors';
import request from 'sync-request-curl';

try {
request('GET', 'https://google.fake.url.com');
Expand All @@ -450,15 +413,16 @@ export class CurlError extends Error {
constructor(code: number, message: string) {
super(message);
if (code < 1 || code > 99) {
throw new Error(`CurlError code must be between 1 and 99. Given: ${code}`);
throw new Error(
`CurlError code must be between 1 and 99. Given: ${code}`
);
}
this.code = code;
Object.setPrototypeOf(this, CurlError.prototype);
}
}
```


## 3. License

<details closed>
Expand Down Expand Up @@ -501,6 +465,7 @@ For installation issues, be sure to review the [Windows Build Requirements](http
In addition, your requests may unexpectedly fail with a [Libcurl Error](https://curl.se/libcurl/c/libcurl-errors.html) (code 60, CURLE_PEER_FAILED_VERIFICATION) when using NodeJS natively on Windows.

The reason is covered in the below resources:

- https://github.com/JCMais/node-libcurl/issues/301
- https://stackoverflow.com/a/34883260/22324694

Expand All @@ -513,6 +478,7 @@ Alternatively, consider using [Windows Subsystem for Linux (WSL)](https://learn.
The build for MacOS may fail during the installation process.

In most cases, this can be fixed by following these Github issues:

- https://github.com/JCMais/node-libcurl/issues/296
- https://github.com/JCMais/node-libcurl/issues/382

Expand All @@ -521,6 +487,7 @@ and carefully reviewing the [MacOS Build Requirements](https://github.com/JCMais
### 4.3. Linux

In rare cases, the build for distributions such as Debian or Ubuntu may fail. This is attributed to missing dependencies such as Python or Libcurl. Below are some related issues:

- https://github.com/JCMais/node-libcurl/issues/374
- https://github.com/JCMais/node-libcurl/issues/376

Expand All @@ -534,4 +501,4 @@ See [sync-request](https://www.npmjs.com/package/sync-request) for the original

**sync-request-curl** was designed to work with UNIX-like systems for UNSW students enrolled in [COMP1531 Software Engineering Fundamentals](https://webcms3.cse.unsw.edu.au/COMP1531/23T2/outline). It has been tested on Alpine, Arch, Debian and Ubuntu Linux and is compatible with Windows/MacOS.

Please note that this library's primary goal is to simplify the learning of JavaScript for novice programmers, hence its synchronous nature. However, we recommend to **always use an [asynchronous alternative](https://blog.appsignal.com/2024/09/11/top-5-http-request-libraries-for-nodejs.html)** where possible.
Please note that this library's primary goal is to simplify the learning of JavaScript for novice programmers, hence its synchronous nature. However, we recommend to **always use an [asynchronous alternative](https://blog.appsignal.com/2024/09/11/top-5-http-request-libraries-for-nodejs.html)** where possible.
Loading