Subscribe to PHP Freaks RSS

Pretty print curl -i

syndicated from planet-php.net on October 11, 2017

My favourite tool for working with APIs is curl and in particular, I like using the -I switch to view the status line and headers too:

This generates an output that looks like this:

$ curl -i https://api.joind.in
HTTP/1.1 200 OK
Date: Wed, 04 Oct 2017 09:51:46 GMT
Server: Apache
X-Powered-By: PHP/5.6.4
Access-Control-Allow-Origin: *
Content-Length: 363
Content-Type: application/json; charset=utf8

{"events":"https:\/\/api.joind.in\/v2.1\/events","hot-events":"https:\/\/api.joind.in\/v2.1\/events?filter=hot","upcoming-events":"https:\/\/api.joind.in\/v2.1\/events?filter=upcoming","past-events":"https:\/\/api.joind.in\/v2.1\/events?filter=past","open-cfps":"https:\/\/api.joind.in\/v2.1\/events?filter=cfp","docs":"http:\/\/joindin.github.io\/joindin-api\/"}

What I would like to do is pretty print the body if it's JSON or XML so that it's easier to read. There are some tools out there, like jq which will format JSON, but they can't seem to cope if the first part of the string is not JSON:

$ curl -i https://api.joind.in | jq
parse error: Invalid numeric literal at line 1, column 9

Solution 1: Use -D flag

The easiest solution is to use the -D flag to send the headers to stderr:

$ curl -s -D "/dev/stderr" https://api.joind.in | jq
HTTP/1.1 200 OK
Date: Wed, 04 Oct 2017 14:56:29 GMT
Server: Apache
X-Powered-By: PHP/5.6.4
Access-Control-Allow-Origin: *
Content-Length: 363
Content-Type: application/json; charset=utf8

{ "events": "https://api.joind.in/v2.1/events", "hot-events": "https://api.joind.in/v2.1/events?filter=hot", "upcoming-events": "https://api.joind.in/v2.1/events?filter=upcoming", "past-events": "https://api.joind.in/v2.1/events?filter=past", "open-cfps": "https://api.joind.in/v2.1/events?filter=cfp", "docs": "http://joindin.github.io/joindin-api/" }

To make this easier, you can add to your ~/.curlrc. Mine looks like this:

$ cat .curlrc
 -w "\n"
 silent
 -D /dev/stderr

Now you never need to think about it.

Solution 2: A simple script to pipe the output through

Another way to do it is via a script that knows where the body starts. We can write this in PHP and it can automatically pretty print XML for us too:

#!/usr/bin/env php
<?php
/**
 * Pretty print XML or JSON, when the output includes HTTP headers from curl -i
 */
$input = file_get_contents('php://stdin');

$input = trim($input); if (substr($input, 0, 5) == 'HTTP/') { // input is probably the output of curl -i define('DELIMITER', "\r\n\r\n"); list($headers, $body) = explode(DELIMITER, $input, 2); if ($body) { echo $headers . DELIMITER; $input = trim($body); } }

// is it JSON? if ($data = json_decode($input)) { echo json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); exit; }

// is it XML? libxml_use_internal_errors(true); if ($simpleXML = simplexml_load_string($input)) { $dom = dom_import_simplexml($simpleXML)->ownerDocument; $dom->formatOutput = true; echo $dom->saveXML(); exit; }

// it's something else! echo $input;

Save this as as /usr/local/prettyprint, make it executable with chmod a+w /usr/bin/prettyprint and you're good to go:

$ curl -i https://api.joind.in | prettyprint
HTTP/1.1 200 OK
Date: Wed, 04 Oct 2017 09:57:07 GMT
Server: Apache
X-Powered-By: PHP/5.6.4
Access-Control-Allow-Origin: *
Content-Length: 363
Content-Type: application/json; charset=utf8

{ "events": "https://api.joind.in/v2.1/events", "hot-events": "https://api.joind.in/v2.1/events?filter=hot", "upcoming-events": "https://api.joind.in/v2.1/events?filter=upcoming", "past-events": "https://api.joind.in/v2.1/events?filter=past", "open-cfps": "https://api.joind.in/v2.1/events?filter=cfp", "docs": "http://joindin.github.io/joindin-api/" }

Pick the solution that works best for you!