ForbesLindesay / sync-request

Make synchronous web requests with cross platform support.
MIT License
326 stars 49 forks source link
request synchronous

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.

var request = require('sync-request');
var res = request('GET', 'http://example.com');
console.log(res.getBody());
var request = require('sync-request');
var res = request('GET', 'https://example.com', {
  headers: {
    'user-agent': 'example-user-agent',
  },
});
console.log(res.getBody());
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:

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:

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