1 | # sync-request
|
2 |
|
3 | Make 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
|
16 | request(method, url, options)
|
17 | ```
|
18 |
|
19 | e.g.
|
20 |
|
21 | ```js
|
22 | var request = require('sync-request');
|
23 | var res = request('GET', 'http://example.com');
|
24 | console.log(res.getBody());
|
25 | ```
|
26 |
|
27 | **Method:**
|
28 |
|
29 | An HTTP method (e.g. `GET`, `POST`, `PUT`, `DELETE` or `HEAD`). It is not case sensitive.
|
30 |
|
31 | **URL:**
|
32 |
|
33 | A 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` - Can be `'memory'` or `'file'`, and enables a local cache of content. A separate process is still spawned even for cache requests.
|
42 |
|
43 | **Returns:**
|
44 |
|
45 | A `Response` object.
|
46 |
|
47 | 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:
|
48 |
|
49 | - `statusCode` - a number representing the HTTP status code
|
50 | - `headers` - http response headers
|
51 | - `body` - a string if in the browser or a buffer if on the server
|
52 |
|
53 | It also has a method `res.getBody(encoding?)` which looks like:
|
54 |
|
55 | ```js
|
56 | function getBody(encoding) {
|
57 | if (this.statusCode >= 300) {
|
58 | var err = new Error('Server responded with status code ' + this.statusCode + ':\n' + this.body.toString(encoding));
|
59 | err.statusCode = this.statusCode;
|
60 | err.headers = this.headers;
|
61 | err.body = this.body;
|
62 | throw err;
|
63 | }
|
64 | return encoding ? this.body.toString(encoding) : this.body;
|
65 | }
|
66 | ```
|
67 |
|
68 | ## How is this possible?
|
69 |
|
70 | Internally, 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.
|
71 |
|
72 | The 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.
|
73 |
|
74 | ## License
|
75 |
|
76 | MIT
|