UNPKG

3.61 kBMarkdownView Raw
1# sync-request
2
3Make synchronous web requests with cross platform support.
4
5[![Build Status](https://img.shields.io/travis/ForbesLindesay/sync-request/master.svg)](https://travis-ci.org/ForbesLindesay/sync-request)
6[![Dependency Status](https://img.shields.io/gemnasium/ForbesLindesay/sync-request.svg)](https://gemnasium.com/ForbesLindesay/sync-request)
7[![NPM version](https://img.shields.io/npm/v/sync-request.svg)](https://www.npmjs.org/package/sync-request)
8
9## Installation
10
11 npm install sync-request
12
13## Usage
14
15```js
16request(method, url, options)
17```
18
19e.g.
20
21```js
22var request = require('sync-request');
23var res = request('GET', 'http://example.com');
24console.log(res.getBody());
25```
26
27**Method:**
28
29An HTTP method (e.g. `GET`, `POST`, `PUT`, `DELETE` or `HEAD`). It is not case sensitive.
30
31**URL:**
32
33A url as a string (e.g. `http://example.com`). Relative URLs are allowed in the browser.
34
35**Options:**
36
37 - `qs` - an object containing querystring values to be appended to the uri
38 - `headers` - http headers (default: `{}`)
39 - `body` - body for PATCH, POST and PUT requests. Must be a `Buffer` or `String` (only strings are accepted client side)
40 - `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.
41 - `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
42 - `followRedirects` - defaults to `true` but can be explicitly set to `false` on node.js to prevent then-request following redirects automatically.
43 - `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.
44
45**Returns:**
46
47A `Response` object.
48
49Note 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:
50
51 - `statusCode` - a number representing the HTTP status code
52 - `headers` - http response headers
53 - `body` - a string if in the browser or a buffer if on the server
54
55It also has a method `res.getBody(encoding?)` which looks like:
56
57```js
58function getBody(encoding) {
59 if (this.statusCode >= 300) {
60 var err = new Error('Server responded with status code ' + this.statusCode + ':\n' + this.body.toString(encoding));
61 err.statusCode = this.statusCode;
62 err.headers = this.headers;
63 err.body = this.body;
64 throw err;
65 }
66 return encoding ? this.body.toString(encoding) : this.body;
67}
68```
69
70## How is this possible?
71
72Internally, this uses a separate worker process that is run using either [childProcess.spawnSync](http://nodejs.org/docs/v0.11.13/api/child_process.html#child_process_child_process_spawnsync_command_args_options) if available, or falling back to [spawn-sync](https://www.npmjs.org/package/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.
73
74The worker then makes the actual request using [then-request](https://www.npmjs.org/package/then-request) so this has almost exactly the same API as that.
75
76This 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.
77
78## License
79
80 MIT