6.1.0 • Published 5 years ago

sync-request v6.1.0

Weekly downloads
322,015
License
MIT
Repository
github
Last release
5 years ago

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.

Build Status Dependency Status NPM version

Installation

npm install sync-request

Usage

request(method, url, options);

e.g.

  • GET request without options
var request = require('sync-request');
var res = request('GET', 'http://example.com');
console.log(res.getBody());
  • GET request with options
var request = require('sync-request');
var res = request('GET', 'https://example.com', {
  headers: {
    'user-agent': 'example-user-agent',
  },
});
console.log(res.getBody());
  • POST request to a JSON endpoint
var request = require('sync-request');
var res = request('POST', 'https://example.com/create-user', {
  json: {username: 'ForbesLindesay'},
});
var 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 uri
  • headers - http headers (default: {})
  • body - body for PATCH, POST and PUT requests. Must be a Buffer or String (only strings are accepted client side)
  • json - sets body but to JSON representation of value and adds Content-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.js
  • followRedirects - defaults to true but can be explicitly set to false 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 if null).
  • gzip - defaults to true but can be explicitly set to false 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) - calls req.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 to true to retry when the request errors or returns a status code greater than or equal to 400
  • retryDelay (default: 200) - the delay between retries in milliseconds
  • maxRetries (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 code
  • headers - http response headers
  • body - a string if in the browser or a buffer if on the server

It also has a method res.getBody(encoding?) which looks like:

function getBody(encoding) {
  if (this.statusCode >= 300) {
    var err = new Error(
      'Server responded with status code ' +
        this.statusCode +
        ':\n' +
        this.body.toString(encoding)
    );
    err.statusCode = this.statusCode;
    err.headers = this.headers;
    err.body = this.body;
    throw err;
  }
  return encoding ? this.body.toString(encoding) : this.body;
}

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

warjssample-wdio-cucumber-qatouch-reporterrollup-plugin-shockysync-request-rappertwitch-api-v5snewman-testrail-reporter-litepurascents-scriptscities-and-towns-of-hungarypayment-botts-studio-procedure-html-reportdyndocreq2doccookeyohmproceduraldoc-graphql-builderseologs-detect-log-filesbookipi-serveroptimuscloud-typescript-client@audio-project-manager/apm-cli@studiorack/studiorack-clishaman-configmtxserv-noderecatchabletypescript-v2subdawndwndev-bbop-rest-managerwikisetsadamant-rest-apidonedone.jsno-ones-onion-nodezuix-cli@blinkz/webpack-isomorphic-toolssinopia_aclmindv-clivisitjsdjango-cdkevg-math-basegulp-essentialswebhookdelmexc-sdkprogbase@infinitebrahmanuniverse/nolb-synceasyteach-howest@arnav13/graph-clicosmos-crypto-lib@mitjafelicijan/unfoldonlinecheckwriter-mailcheckstreammeaiosdk-js@everything-registry/sub-chunk-2866duscriptunitsx-js@apostrophecms/cli@asposecloud/aspose-html-cloudbbpolymerbbop-rest-managerbeame-insta-sslbeame-sdkbayardbehind-bars@aliyun-sls/build-tool@aigotsrl/vault-envbirdknifebicornbetterlyearn2@algoasaurujs/wasm-loader@aleohq/sdkbitpost@aws-quickstart/eks-blueprints@aws-quickstart/ssp-amazon-eksbitbar-cli@axe-core/watcher@cobnl/conventional-changelog@considonet/g-fontasticbundlercloudcms-packagercloudwatch-winston-wrappercloudflow-apicli-plusblog-remark@basic-orange/updatecropsplugin@beforeyoubid/jsx-mail-components@dapplearning/graph-cli@daikonjs/core@d-cat/mocks-google-analytics-jestcom.gamesbykevin.commonfunctions@curefatih-jf/hdom@dazlab-team/angular-playable-buildercontents-file-loaderconsul-lookupconventional-changelog-cobcookeylang-ohm@blacklight-cms/clibs-rerunbs-pro-clibriksbrowserstack-capabilitiesbrt-i18n-pluginbox-jsbox-js-testbreezetsbreezets-cli
6.1.0

5 years ago

6.0.0

6 years ago

5.0.0

6 years ago

4.1.0

7 years ago

4.0.3

7 years ago

4.0.2

7 years ago

4.0.1

7 years ago

4.0.0

7 years ago

3.0.1

8 years ago

3.0.0

8 years ago

2.2.0

8 years ago

2.1.0

8 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.0.2

9 years ago

1.0.1

10 years ago

1.0.0

10 years ago