Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
sync-request-curl
Advanced tools
Fast way to send synchronous web requests in NodeJS. API is a subset of sync-request. Leverages node-libcurl for high performance. Cannot be used in a browser.
Make synchronous web requests similar to sync-request, but 20 times more quickly.
Leverages node-libcurl for performance instead of spawning child processes like sync-request.
This library was designed to run on NodeJS. It will not work in a browser.
npm install sync-request-curl
Please refer to the compatibility section for known issues and workarounds for Windows, MacOS and Linux.
request(method, url, options);
GET
request without options
import request from 'sync-request-curl';
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);
GET
request with query string parameters
import request from 'sync-request-curl';
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);
POST
request with headers and JSON payload
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'
},
}
);
console.log('Status Code:', res.statusCode);
const jsonBody = JSON.parse(res.body.toString());
console.log('Returned JSON Object:', jsonBody);
See sync-request for the original documentation. All Libcurl Errors will contain a non-zero integer code that can be looked up here.
Please note that this library only supports a subset of the original features which are summarised below.
HTTP method (of type HttpVerb
)
PUT
/POST
/GET
/DELETE
URL as a string
Only the following options from sync-request are supported for the time being:
Option | Description | Example | Default |
---|---|---|---|
qs | An object containing query string parameters which will be appended to the URL. |
{ message: 'Hi!' } | undefined |
headers | HTTP headers for the request. |
{ token: 'abcde' } | undefined |
json |
Sets the body as a JSON representation of the value and automatically adds Content-type: application/json to the header. |
{ name: 'Tam', course: 1531 } | undefined |
body |
String body for PATCH, POST and PUT requests. We recommended using json instead for JSON payloads, otherwise the Content-Type will need to be set manually.
|
JSON.stringify({ name: 'Tam', course: 1531 }) | undefined |
timeout | Times out if no response is returned within the given number of milliseconds. | 2000 | 0 (no timeout) |
followRedirects | Sets whether redirects (status code 302) should be followed automatically. | false | true |
maxRedirects | Sets the maximum number of redirects to follow before throwing an Error. | 3 | -1 (no limit) |
Below are some additional options available from node-libcurl:
Option | Description | Example | Default |
---|---|---|---|
insecure | Set to true to send insecure requests. This can be useful on Windows which may have SSL issues (Libcurl code 60). | true | false |
setEasyOptions | Optional callback to set additional curl options for the Easy Interface. This has priority over existing options. |
(curl, opt) => { curl.setOpt( opt.URL, 'www.ck' ); }; | undefined |
In src/types.ts, the Options
interface following is defined as:
export interface Options {
/*
* sync-request options
*/
headers?: IncomingHttpHeaders;
qs?: { [key: string]: any };
json?: any;
body?: string | Buffer;
timeout?: number;
followRedirects?: boolean;
maxRedirects?: number;
/*
* node-libcurl options
*/
insecure?: boolean;
setEasyOptions?: SetEasyOptionCallback;
}
statusCode
- a number representing the HTTP status code (e.g. 200
, 400
, 401
, 403
)headers
- HTTP response headersbody
- a string or buffer - use body.toString()
for common use cases in combination with JSON.parse()
getBody
- a function with an optional encoding
argument that returns the body
if encoding
is undefined, otherwise body.toString(encoding)
. If statusCode >= 300
, an Error
is thrown instead.url
- the final URL used in the request after all redirections and with the query string parameters appended.In src/types.ts, the Response
interface is defined as:
export interface Response {
statusCode: number;
headers: IncomingHttpHeaders;
body: string | Buffer;
getBody: (encoding?: BufferEncoding) => string | Buffer; // simplified
url: string;
}
Copyright (c) 2023 Khiet Tam Nguyen
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the “Software”),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALING S IN THE SOFTWARE.
For installation issues, be sure to review the Windows Build Requirements from node-libcurl's documentation.
In addition, your requests may unexpectedly fail with a Libcurl Error (code 60, CURLE_PEER_FAILED_VERIFICATION) when using NodeJS natively on Windows.
The reason is covered in the below resources:
One quick workaround is to set the insecure
option to true
when sending your requests. This is the same as setting the Libcurl Easy's equivalent SSL_VERIFYPEER to 0
, or using curl
in the command line with the -k
or --insecure
option.
Alternatively, consider using Windows Subsystem for Linux (WSL).
The build for MacOS may fail during the installation process.
In most cases, this can be fixed by following these Github issues:
and carefully reviewing the MacOS Build Requirements from node-libcurl's documentation. Otherwise, we recommend uninstalling this library and installing sync-request.
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:
Be sure to also check the Linux Build Requirements from node-libcurl's documentation.
This library was developed mainly to improve performance with sending synchronous requests in NodeJS.
It was designed to work with UNIX-like systems for UNSW students enrolled in COMP1531 Software Engineering Fundamentals.
It has been tested to be working on Arch & Debian Linux and is compatible with Windows/MacOS.
FAQs
Fast way to send synchronous web requests in NodeJS. API is a subset of sync-request. Leverages node-libcurl for high performance. Cannot be used in a browser.
The npm package sync-request-curl receives a total of 32,229 weekly downloads. As such, sync-request-curl popularity was classified as popular.
We found that sync-request-curl demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.