Skip to content
This repository was archived by the owner on Jul 12, 2019. It is now read-only.

Pulling in upstream updates to restler #3

Merged
merged 79 commits into from
Mar 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
a37135d
Correct README to specify 'delete' instead of 'del' for 'method' option.
evdb May 17, 2012
da1f3da
Added HTTP Code 307 (Temp redirect) to list of redirect codes
May 18, 2012
8a2ca18
Changed EventEmitter inheritens to Node0.10 style.
macrauder Mar 13, 2013
fcbed66
Fix Data object support.
mloar Jun 2, 2013
85d7862
Add Content-Length and properly handle Buffers.
mloar Jun 2, 2013
954774e
Added support for auth without password
jasonpincin Jun 30, 2013
9d455ff
Merge pull request #113 from eric-wieser/fixed-inheritance
danwrong Aug 29, 2013
d60b988
Added missing documentation for PATCH HTTP method
h2non Aug 31, 2013
e2b0fc6
timeout event, and timeout option implemented. added docs & example
bosky101 Sep 4, 2013
2e03125
refactored code into _fireCancelTimeout, which is now also called whe…
bosky101 Sep 4, 2013
628979c
Fixes #115: Allow users to pass through rejectUnauthorized
terinjokes Oct 4, 2013
b7f3c9f
Ugh, idiot
terinjokes Oct 4, 2013
669ede2
Updated packages to latest version.
easternbloc Dec 6, 2013
dec98fd
Merge pull request #134 from danwrong/feature/updatePackagesAndFixTests
easternbloc Dec 6, 2013
12dcbdc
Update README.md
easternbloc Dec 6, 2013
514632c
Merge pull request #128 from h2non/master
easternbloc Dec 6, 2013
ead274e
Adding repository to the package.json.
easternbloc Dec 6, 2013
aff9340
Merge pull request #136 from danwrong/feature/repositoryField
easternbloc Dec 6, 2013
7d739cb
Merge pull request #132 from terinjokes/ignore-self-sign
easternbloc Dec 6, 2013
731faa1
Set Content-Length for empty data response.
easternbloc Dec 9, 2013
5f458e9
Merge pull request #137 from danwrong/feature/emptyContentLength
easternbloc Dec 9, 2013
8141395
Merge pull request #122 from jasonpincin/master
easternbloc Dec 9, 2013
c0baab5
Allow empty password auth header with test.
easternbloc Dec 9, 2013
75e9dba
Merge pull request #138 from danwrong/feature/basicauthwithemptypassword
easternbloc Dec 9, 2013
41854b1
Adding node-querystring due to incomplete qs in node code.
easternbloc Dec 9, 2013
f5cecfa
Merge pull request #139 from danwrong/feature/nodeqs
easternbloc Dec 9, 2013
e7a424a
Version bump in accoradance with semvar.
easternbloc Dec 9, 2013
314c4ad
Merge pull request #140 from danwrong/feature/versionBump
easternbloc Dec 9, 2013
051151e
Updates to documentation and supported node engines.
easternbloc Dec 10, 2013
4c8851f
Merge pull request #141 from danwrong/feature/updatedDocs
easternbloc Dec 10, 2013
05c6465
Minor updates to readme
easternbloc Dec 10, 2013
94999aa
Merge pull request #142 from danwrong/feature/updatedDocs
easternbloc Dec 10, 2013
d3deca9
As we now support node 0.10.x upwards we just require zlib instead of…
easternbloc Dec 10, 2013
0855d9d
Merge pull request #143 from danwrong/feature/correctPackages
easternbloc Dec 10, 2013
aad019f
Merge pull request #86 from bgkittrell/master
easternbloc Dec 10, 2013
5925ded
Merge pull request #85 from evdb/patch-1
easternbloc Dec 10, 2013
0ca56f7
Merge pull request #130 from bosky101/timeout
easternbloc Dec 11, 2013
56bf997
Fixed a few jshint issues.
easternbloc Dec 11, 2013
5409273
Added a simple test for timeouts
easternbloc Dec 11, 2013
9b59588
Merge pull request #144 from danwrong/feature/timeoutTests
easternbloc Dec 11, 2013
2af60e1
Update README.md
easternbloc Dec 11, 2013
5b07d14
Update README.md
easternbloc Dec 11, 2013
f17b670
Move to a purejs implementation of iconv
easternbloc Dec 11, 2013
62abff6
Merge pull request #146 from danwrong/feature/purejsiconv
easternbloc Dec 12, 2013
2c658f1
Version bump 3.1.0
easternbloc Dec 12, 2013
e122f9d
Update README.md
easternbloc Dec 12, 2013
c295a6b
Added an npm badge
easternbloc Dec 12, 2013
394cc0d
Update README.md
easternbloc Dec 13, 2013
a264c33
don't perform callback in a try/catch block after parsing JSON
Dec 13, 2013
f1d0a95
squash! don't perform callback in a try/catch block after parsing JSON
Dec 13, 2013
b82848e
Merge pull request #149 from pmaccart/no_callback_trycatch
easternbloc Dec 16, 2013
e24f086
Adding json handing to put requests
mcameron Feb 16, 2014
a0228f1
Updating docs
mcameron Feb 16, 2014
7a696b1
Updating docs
mcameron Feb 16, 2014
2b5aa08
Adding tests
mcameron Feb 16, 2014
20448dc
Merge pull request #158 from mcameron/master
easternbloc Feb 17, 2014
cd99af2
Version bump for latest release.
easternbloc Feb 17, 2014
2fcf07b
Merge pull request #159 from danwrong/feature/release
easternbloc Feb 17, 2014
09111fc
Add missing doc for rejectUnauthorized option
Feb 20, 2014
a2dd9d8
Fix link to Node Buffer API
Mar 9, 2014
dd99071
Merge pull request #164 from gustavohenke/patch-1
easternbloc Mar 10, 2014
7de0976
Merge pull request #160 from curtisallen/master
easternbloc Mar 10, 2014
c0091b4
Update README.md
easternbloc Apr 9, 2014
d5fc490
Add OAuth support
viart Apr 14, 2014
adb1eef
Merge pull request #170 from viart/bearer-support
easternbloc Apr 14, 2014
6f23932
Update to latest upstream from branch 'upstream/master'.
lapo-luchini Apr 23, 2014
be34cbc
Add test case for mloar's PR danwrong/restler#119.
lapo-luchini Apr 23, 2014
e6c704b
Merge pull request #172 from lapo-luchini/data
easternbloc Apr 24, 2014
017710a
Version bump
easternbloc Apr 24, 2014
a5901e6
Allow writing buffer directly to req body
jutaz Jun 17, 2014
6ef99d9
Updating to qs@1.2.0
pdehaan Aug 9, 2014
47e1b63
Added test to send buffer
jutaz Aug 14, 2014
7584f44
Merge pull request #188 from jutaz/master
easternbloc Jan 22, 2015
56c2a77
Merge pull request #187 from pdehaan/patch-1
easternbloc Jan 22, 2015
0cf5fbe
Cleaner at-a-glace stats/info
jbender Jan 22, 2015
79a08d9
Keep old style for headers
jbender Jan 22, 2015
64e80d9
Merge pull request #200 from jbender/patch-1
easternbloc Feb 11, 2015
0e2d552
Merge remote-tracking branch 'upstream/master' into zackb-massive
Mar 30, 2015
9a4d5a6
missing one change in merge
Mar 30, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 49 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
Restler
Restler [![NPM Version](https://img.shields.io/npm/v/restler.svg?style=flat)](https://www.npmjs.com/package/restler) ![Node Version](https://img.shields.io/node/v/restler.svg?style=flat) ![Downloads](https://img.shields.io/npm/dm/restler.svg?style=flat)
=======

(C) Dan Webb (dan@danwebb.net/@danwrong) 2011, Licensed under the MIT-LICENSE

An HTTP client library for node.js (0.6.x and up). Hides most of the complexity of creating and using http.Client.
An HTTP client library for node.js. Hides most of the complexity of creating and using http.Client.

**Release 2.x.x** is dedicated to modifying how errors are handled and emitted. Currently errors are being fired as an on 'error' event but as [@ctavan](https://github.com/ctavan) pointed out on [issue #36](https://github.com/danwrong/restler/pull/36) a better approach (and more commonly in vogue now) would be to pass the error obj to the callback.
See [Version History](https://github.com/danwrong/restler/wiki/Version-History) for changes

Ths change will inevitably affect those using older < 0.2.x versions of restler. Those not ready to upgrade yet are encouraged to stay on the 0.2.x version.
Installing
----------

See [Version History](https://github.com/danwrong/restler/wiki/Version-History) for changes
```
npm install restler
```

Running the tests
-----------------

```
npm test
```


Features
Expand All @@ -18,15 +28,15 @@ Features
* Easy interface for common operations via http.request
* Automatic serialization of post data
* Automatic serialization of query string data
* Automatic deserialization of XML, JSON and YAML responses to JavaScript objects (if you have js-yaml and/or xml2js in the require path)
* Automatic deserialization of XML, JSON and YAML responses to JavaScript objects
* Provide your own deserialization functions for other datatypes
* Automatic following of redirects
* Send files with multipart requests
* Transparently handle SSL (just specify https in the URL)
* Deals with basic auth for you, just provide username and password options
* Simple service wrapper that allows you to easily put together REST API libraries
* Transparently handle content-encoded responses (gzip, deflate) (requires node 0.6+)
* Transparently handle different content charsets via [iconv](https://github.com/bnoordhuis/node-iconv) (if available)
* Transparently handle content-encoded responses (gzip, deflate)
* Transparently handle different content charsets via [iconv-lite](https://github.com/ashtuchkin/iconv-lite)


API
Expand All @@ -43,6 +53,7 @@ Basic method to make a request of any type. The function returns a RestRequest o
* `fail: function(data, response)` - emitted when the request was successful, but 4xx status code returned. Gets passed the response data and the response object as arguments.
* `error: function(err, response)` - emitted when some errors have occurred (eg. connection aborted, parse, encoding, decoding failed or some other unhandled errors). Gets passed the `Error` object and the response object (when available) as arguments.
* `abort: function()` - emitted when `request.abort()` is called.
* `timeout: function(ms)` - when a request takes more than the timeout option eg: {timeout:5000}, the request will be aborted. error and abort events will not be called, instead timeout will be emitted.
* `2XX`, `3XX`, `4XX`, `5XX: function(data, response)` - emitted for all requests with response codes in the range (eg. `2XX` emitted for 200, 201, 203).
* <code><i>actual response code</i>: function(data, response)</code> - emitted for every single response code (eg. 404, 201, etc).

Expand Down Expand Up @@ -73,6 +84,10 @@ Create a DELETE request.

Create a HEAD request.

### patch(url, options)

Create a PATCH request.

### json(url, data, options)

Send json `data` via GET method.
Expand All @@ -81,6 +96,9 @@ Send json `data` via GET method.

Send json `data` via POST method.

### putJson(url, data, options)

Send json `data` via PUT method.

### Parsers

Expand All @@ -98,45 +116,53 @@ All of these attempt to turn the response into a JavaScript object. In order to

### Options

* `method` Request method, can be get, post, put, del. Defaults to `"get"`.
* `method` Request method, can be get, post, put, delete. Defaults to `"get"`.
* `query` Query string variables as a javascript object, will override the querystring in the URL. Defaults to empty.
* `data` The data to be added to the body of the request. Can be a string or any object.
Note that if you want your request body to be JSON with the `Content-Type: application/json`, you need to
`JSON.stringify` your object first. Otherwise, it will be sent as `application/x-www-form-urlencoded` and encoded accordingly.
Also you can use `json()` and `postJson()` methods.
* `parser` A function that will be called on the returned data. Use any of predefined `restler.parsers`. See parsers section below. Defaults to `restler.parsers.auto`.
* `encoding` The encoding of the request body. Defaults to `"utf8"`.
* `decoding` The encoding of the response body. For a list of supported values see [Buffers](http://nodejs.org/docs/latest/api/buffers.html#buffers). Additionally accepts `"buffer"` - returns response as `Buffer`. Defaults to `"utf8"`.
* `decoding` The encoding of the response body. For a list of supported values see [Buffers](http://nodejs.org/api/buffer.html#buffer_buffer). Additionally accepts `"buffer"` - returns response as `Buffer`. Defaults to `"utf8"`.
* `headers` A hash of HTTP headers to be sent. Defaults to `{ 'Accept': '*/*', 'User-Agent': 'Restler for node.js' }`.
* `username` Basic auth username. Defaults to empty.
* `password` Basic auth password. Defaults to empty.
* `accessToken` OAuth Bearer Token. Defaults to empty.
* `multipart` If set the data passed will be formated as `multipart/form-encoded`. See multipart example below. Defaults to `false`.
* `client` A http.Client instance if you want to reuse or implement some kind of connection pooling. Defaults to empty.
* `followRedirects` If set will recursively follow redirects. Defaults to `true`.
* `timeout` If set, will emit the timeout event when the response does not return within the said value (in ms)
* `rejectUnauthorized` If true, the server certificate is verified against the list of supplied CAs. An 'error' event is emitted if verification fails. Verification happens at the connection level, before the HTTP request is sent. Default true.


Example usage
-------------

```javascript
var sys = require('util'),
rest = require('./restler');
var rest = require('./restler');

rest.get('http://google.com').on('complete', function(result) {
if (result instanceof Error) {
sys.puts('Error: ' + result.message);
console.log('Error:', result.message);
this.retry(5000); // try again after 5 sec
} else {
sys.puts(result);
console.log(result);
}
});

rest.get('http://twaud.io/api/v1/users/danwrong.json').on('complete', function(data) {
sys.puts(data[0].message); // auto convert to object
console.log(data[0].message); // auto convert to object
});

rest.get('http://twaud.io/api/v1/users/danwrong.xml').on('complete', function(data) {
sys.puts(data[0].sounds[0].sound[0].message); // auto convert to object
console.log(data[0].sounds[0].sound[0].message); // auto convert to object
});

rest.get('http://someslowdomain.com',{timeout: 10000}).on('timeout', function(ms){
console.log('did not return within '+ms+' ms');
}).on('complete',function(data,response){
console.log('did not time out');
});

rest.post('http://user:pass@service.com/action', {
Expand All @@ -157,7 +183,7 @@ rest.post('https://twaud.io/api/v1/upload.json', {
'sound[file]': rest.file('doug-e-fresh_the-show.mp3', null, 321567, null, 'audio/mpeg')
}
}).on('complete', function(data) {
sys.puts(data.audio_url);
console.log(data.audio_url);
});

// create a service constructor for very easy API wrappers a la HTTParty...
Expand All @@ -174,7 +200,7 @@ Twitter = rest.service(function(u, p) {

var client = new Twitter('danwrong', 'password');
client.update('Tweeting using a Restler service thingy').on('complete', function(data) {
sys.p(data);
console.log(data);
});

// post JSON
Expand All @@ -183,26 +209,12 @@ rest.postJson('http://example.com/action', jsonData).on('complete', function(dat
// handle response
});

```

Running the tests
-----------------
install **[nodeunit](https://github.com/caolan/nodeunit)**

```bash
npm install nodeunit
```

then

```bash
node test/all.js
```

or
// put JSON
var jsonData = { id: 334 };
rest.putJson('http://example.com/action', jsonData).on('complete', function(data, response) {
// handle response
});

```bash
nodeunit test/restler.js
```

TODO
Expand Down
7 changes: 6 additions & 1 deletion lib/multipartform.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ Part.prototype = {
if (this.value.data) {
header = "Content-Disposition: form-data; name=\"" + this.name +
"\"; filename=\"" + this.value.filename + "\"\r\n" +
"Content-Length: " + this.value.data.length + "\r\n" +
"Content-Type: " + this.value.contentType;
} if (this.value instanceof File) {
} else if (this.value instanceof File) {
header = "Content-Disposition: form-data; name=\"" + this.name +
"\"; filename=\"" + this.value.filename + "\"\r\n" +
"Content-Length: " + this.value.fileSize + "\r\n" +
Expand Down Expand Up @@ -127,6 +128,10 @@ Part.prototype = {
});
})(); // reader()
});
} else if (this.value instanceof Data) {
stream.write(this.value.data);
stream.write("\r\n");
callback();
} else {
stream.write(this.value + "\r\n");
callback();
Expand Down
Loading