Welcome, guest | Sign In | My Account | Store | Cart

Save this to "nicerest", chmod +x-it, and pipe your REST API curl calls through this for nicer output. It will notice HTTP headers (curl's -i option) and skips those before attempting to pretty-print the following JSON.

Note: This is currently using node 0.2. I should update for 0.3 changes (I think process.openStdin changed).

JavaScript, 45 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env node
//
// nicerest -- pipe your REST API `curl` calls through this for nicer output
//

var stdin = process.openStdin();
var EventEmitter = require('events').EventEmitter;

var buffer = "";

stdin.setEncoding('utf8');
stdin.on('data', function (chunk) {
    buffer += chunk;
});

stdin.on('end', function () {
    if (buffer.slice(0,5) === "HTTP/") {
        var index = buffer.indexOf('\r\n\r\n');
        var sepLen = 4;
        if (index == -1) {
            index = buffer.indexOf('\n\n');
            sepLen = 2;
        }
        if (index != -1) {
            process.stdout.write(buffer.slice(0, index+sepLen));
            buffer = buffer.slice(index+sepLen);
        }
    }
    if (buffer[0] === '{' || buffer[0] === '[') {
        try {
            process.stdout.write(JSON.stringify(JSON.parse(buffer), null, 2));
            process.stdout.write('\n');
        } catch(ex) {
            process.stdout.write(buffer);
            if (buffer[buffer.length-1] !== "\n") {
                process.stdout.write('\n');
            }
        }
    } else {
        process.stdout.write(buffer);
        if (buffer[buffer.length-1] !== "\n") {
            process.stdout.write('\n');
        }
    }
});

An example using a twitter search:

$ curl -is http://search.twitter.com/search.json?q=node.js | nicerest 
HTTP/1.1 200 OK
Date: Tue, 18 Jan 2011 22:53:34 GMT
Server: hi
Status: 200 OK
X-Served-From: slc1-aat-25-sr1
X-Runtime: 0.03050
Content-Type: application/json; charset=utf-8
X-Timeline-Cache-Hit: Hit
X-Served-By: slc1-aax-36-sr4.prod.twitter.com
Cache-Control: max-age=15, must-revalidate, max-age=300
Expires: Tue, 18 Jan 2011 22:58:34 GMT
Content-Length: 9261
Vary: Accept-Encoding
X-Varnish: 1099164977
Age: 0
Via: 1.1 varnish
X-Cache-Svr: slc1-aax-36-sr4.prod.twitter.com
X-Cache: MISS
Connection: close

{
    "results": [
        {
            "from_user_id_str": "76072",
            "profile_image_url": "http://a1.twimg.com/profile_images/656183228/4270790710_dcdf89cf13_b_normal.jpg",
            "created_at": "Tue, 18 Jan 2011 22:50:45 +0000",
            "from_user": "reid",
            "id_str": "27498152823099392",
            "metadata": {
                "result_type": "recent"
            },
            "to_user_id": null,
            "text": "You shouldn't add the word "node" to the library name that you require(). If it's for Node.js, that's what "engines" in package.json is for.",
            "id": 27498152823099390,
            "from_user_id": 76072,
            "geo": null,
            "iso_language_code": "en",
            "to_user_id_str": null,
            "source": "<a href="http://twitter.com/">web</a>"
        },
...

2 comments

Trent Mick (author) 13 years, 2 months ago  # | flag
Tim Belkin 9 years, 5 months ago  # | flag

There's also some cool online tools to pretty print JSON. I really like www.jsonprettyprint.net for example.

Cheers, Tim

Created by Trent Mick on Tue, 18 Jan 2011 (MIT)
JavaScript recipes (69)
Trent Mick's recipes (28)

Required Modules

  • (none specified)

Other Information and Tasks