Make synchronous web requests with cross platform support.
Installations
npm install sync-request
Score
54.4
Supply Chain
97.7
Quality
73.4
Maintenance
100
Vulnerability
99.6
License
Releases
Unable to fetch releases
Developer
ForbesLindesay
Developer Guide
Module System
CommonJS, UMD
Min. Node Version
>=8.0.0
Typescript Support
Yes
Node Version
10.12.0
NPM Version
6.4.1
Statistics
326 Stars
84 Commits
49 Forks
8 Watching
35 Branches
14 Contributors
Updated on 26 Oct 2023
Bundle Size
38.87 kB
Minified
11.99 kB
Minified + Gzipped
Languages
TypeScript (50.88%)
JavaScript (49.12%)
Total Downloads
Cumulative downloads
Total Downloads
141,634,339
Last day
-4.2%
187,533
Compared to previous day
Last week
1%
993,624
Compared to previous week
Last month
12.6%
4,264,877
Compared to previous month
Last year
63.3%
44,013,290
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
3
Dev Dependencies
8
sync-request
Make synchronous web requests with cross-platform support.
Requires at least node 8
N.B. You should not be using this in a production application. In a node.js application you will find that you are completely unable to scale your server. In a client application you will find that sync-request causes the app to hang/freeze. Synchronous web requests are the number one cause of browser crashes. For production apps, you should use then-request, which is exactly the same except that it is asynchronous.
Installation
npm install sync-request
Usage
1request(method, url, options);
e.g.
- GET request without options
1var request = require('sync-request'); 2var res = request('GET', 'http://example.com'); 3console.log(res.getBody());
- GET request with options
1var request = require('sync-request'); 2var res = request('GET', 'https://example.com', { 3 headers: { 4 'user-agent': 'example-user-agent', 5 }, 6}); 7console.log(res.getBody());
- POST request to a JSON endpoint
1var request = require('sync-request'); 2var res = request('POST', 'https://example.com/create-user', { 3 json: {username: 'ForbesLindesay'}, 4}); 5var user = JSON.parse(res.getBody('utf8'));
Method:
An HTTP method (e.g. GET
, POST
, PUT
, DELETE
or HEAD
). It is not case sensitive.
URL:
A url as a string (e.g. http://example.com
). Relative URLs are allowed in the browser.
Options:
qs
- an object containing querystring values to be appended to the uriheaders
- http headers (default:{}
)body
- body for PATCH, POST and PUT requests. Must be aBuffer
orString
(only strings are accepted client side)json
- setsbody
but to JSON representation of value and addsContent-type: application/json
. Does not have any affect on how the response is treated.cache
- Set this to'file'
to enable a local cache of content. A separate process is still spawned even for cache requests. This option is only used if running in node.jsfollowRedirects
- defaults totrue
but can be explicitly set tofalse
on node.js to prevent then-request following redirects automatically.maxRedirects
- sets the maximum number of redirects to follow before erroring on node.js (default:Infinity
)allowRedirectHeaders
(default:null
) - an array of headers allowed for redirects (none ifnull
).gzip
- defaults totrue
but can be explicitly set tofalse
on node.js to prevent then-request automatically supporting the gzip encoding on responses.timeout
(default:false
) - times out if no response is returned within the given number of milliseconds.socketTimeout
(default:false
) - callsreq.setTimeout
internally which causes the request to timeout if no new data is seen for the given number of milliseconds. This option is ignored in the browser.retry
(default:false
) - retry GET requests. Set this totrue
to retry when the request errors or returns a status code greater than or equal to 400retryDelay
(default:200
) - the delay between retries in millisecondsmaxRetries
(default:5
) - the number of times to retry before giving up.
These options are passed through to then-request, so any options that work for then-request should work for sync-request (with the exception of custom and memory caching strategies, and passing functions for handling retries).
Returns:
A Response
object.
Note that even for status codes that represent an error, the request function will still return a response. You can call getBody
if you want to error on invalid status codes. The response has the following properties:
statusCode
- a number representing the HTTP status codeheaders
- http response headersbody
- a string if in the browser or a buffer if on the server
It also has a method res.getBody(encoding?)
which looks like:
1function getBody(encoding) { 2 if (this.statusCode >= 300) { 3 var err = new Error( 4 'Server responded with status code ' + 5 this.statusCode + 6 ':\n' + 7 this.body.toString(encoding) 8 ); 9 err.statusCode = this.statusCode; 10 err.headers = this.headers; 11 err.body = this.body; 12 throw err; 13 } 14 return encoding ? this.body.toString(encoding) : this.body; 15}
Common Problems
Could not use "nc", falling back to slower node.js method for sync requests.
If you are running on windows, or some unix systems, you may see the message above. It will not cause any problems, but will add an overhead of ~100ms to each request you make. If you want to speed up your requests, you will need to install an implementation of the nc
unix utility. This usually done via something like:
apt-get install netcat
How is this possible?
Internally, this uses a separate worker process that is run using childProcess.spawnSync.
The worker then makes the actual request using then-request so this has almost exactly the same API as that.
This can also be used in a web browser via browserify because xhr has built in support for synchronous execution. Note that this is not recommended as it will be blocking.
License
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 existing vulnerabilities detected
Reason
Found 3/22 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 12 are checked with a SAST tool
Score
3.2
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to sync-request
sync-request-curl
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.
ts-sync-request
TypeScript strongly-typed wrapper for sync-request library. Make synchronous http calls from TypeScript.
use-sync-external-store
Backwards compatible shim for React's useSyncExternalStore. Works with any React that supports hooks.
degenerator
Compiles sync functions into async generator functions