JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 815905
  • Score
    100M100P100Q178514F
  • License MIT

Make synchronous web requests

Package Exports

  • sync-request

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sync-request) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

sync-request

Make synchronous web requests with cross platform support.

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());

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 - Can be 'memory' or 'file', and enables a local cache of content. A separate process is still spawned even for cache requests.

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;
}

How is this possible?

Internally, this uses a separate worker process that is run using either childProcess.spawnSync if available, or falling back to spawn-sync if not. The fallback will attempt to install a native module for synchronous execution, and fall back to doing busy waiting for a file to exist. All this ultimatley means that the module is totally cross platform and does not require native code compilation support.

The worker then makes the actual request using then-request so this has almost exactly the same API as that.

License

MIT