Permalink
Browse files

json, postJson methods: added test cases, updated readme (#53)

  • Loading branch information...
1 parent 3f62973 commit 60983d5b051970c2e29448cdb7a7db0daca4d22f @disfated disfated committed Jan 8, 2012
Showing with 67 additions and 5 deletions.
  1. +18 −5 README.md
  2. +16 −0 lib/restler.js
  3. +33 −0 test/restler.js
View
@@ -54,6 +54,18 @@ Create a PUT request.
Create a DELETE request.
+### head(url, options)
+
+Create a HEAD request.
+
+### json(url, data, options)
+
+Send json `data` via GET method.
+
+### postJson(url, data, options)
+
+Send json `data` via POST method.
+
### response parsers
You can give any of these to the parsers option to specify how the response data is deserialized.
@@ -74,6 +86,7 @@ All of these attempt to turn the response into a JavaScript object. In order to
* `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"`.
@@ -142,12 +155,12 @@ client.update('Tweeting using a Restler service thingy').on('complete', function
sys.p(data);
});
-// the JSON post
-rest.post('http://example.com/action', {
- data: JSON.stringify({ id: 334 }),
-}).on('complete', function(data, response) {
- // you can get at the raw response like this...
+// post JSON
+var jsonData = { id: 334 };
+rest.postJson('http://example.com/action', jsonData).on('complete', function(data, response) {
+ // handle response
});
+
```
Running the tests
View
@@ -228,6 +228,20 @@ function head(url, options) {
return request(url, shortcutOptions(options, 'HEAD'));
}
+function json(url, data, options, method) {
+ options = options || {};
+ options.parser = (typeof options.parser !== "undefined") ? options.parser : parsers.auto;
+ options.headers = options.headers || {};
+ options.headers['content-type'] = 'application/json';
+ options.data = JSON.stringify(data);
+ options.method = method || 'GET';
+ return request(url, options);
+}
+
+function postJson(url, data, options) {
+ return json(url, data, options, 'POST');
+}
+
var parsers = {
auto: function(data, callback) {
var contentType = this.headers['content-type'];
@@ -361,6 +375,8 @@ mixin(exports, {
put: put,
del: del,
head: head,
+ json: json,
+ postJson: postJson,
parsers: parsers,
file: multipart.file,
data: multipart.data
View
@@ -254,6 +254,18 @@ function dataResponse(request, response) {
response.writeHead(200);
response.end(Buffer([9, 30, 64, 135, 200]));
break;
+ case '/push-json':
+ var echo = '';
+ request.addListener('data', function(chunk) {
+ echo += chunk.toString('binary');
+ });
+ request.addListener('end', function() {
+ response.writeHead(200, {
+ 'content-type': 'application/json',
+ });
+ response.end(JSON.stringify(JSON.parse(echo)));
+ });
+ break;
default:
response.writeHead(404);
response.end();
@@ -336,6 +348,27 @@ module.exports['Deserialization'] = {
test.equal(data, 'CR5Ah8g=', 'returned: ' + sys.inspect(data));
test.done();
})
+ },
+
+ 'Should post and parse JSON': function(test) {
+ var obj = { secret : 'very secret string' };
+ rest.post(host + '/push-json', {
+ headers: {
+ 'content-type': 'application/json'
+ },
+ data: JSON.stringify(obj)
+ }).on('complete', function(data) {
+ test.equal(obj.secret, data.secret, 'returned: ' + sys.inspect(data));
+ test.done();
+ })
+ },
+
+ 'Should post and parse JSON via shortcut method': function(test) {
+ var obj = { secret : 'very secret string' };
+ rest.postJson(host + '/push-json', obj).on('complete', function(data) {
+ test.equal(obj.secret, data.secret, 'returned: ' + sys.inspect(data));
+ test.done();
+ });
}
};

0 comments on commit 60983d5

Please sign in to comment.