From fbf1cbc164fecde55b023249876a80192af12eb6 Mon Sep 17 00:00:00 2001 From: dantleech Date: Thu, 22 Jan 2015 06:59:41 +0000 Subject: [PATCH] Pretty print JSON --- src/Context/WebApiContext.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Context/WebApiContext.php b/src/Context/WebApiContext.php index f21b24b..cbda063 100644 --- a/src/Context/WebApiContext.php +++ b/src/Context/WebApiContext.php @@ -265,13 +265,23 @@ public function printResponse() { $request = $this->request; $response = $this->response; + $contentType = $response->getHeader('Content-Type'); + $body = $response->getBody(); + + switch($contentType) { + case 'application/json': + $formattedJson = json_encode(json_decode($body), JSON_PRETTY_PRINT); + if ($formattedJson) { + $body = $formattedJson; + } + } echo sprintf( "%s %s => %d:\n%s", $request->getMethod(), $request->getUrl(), $response->getStatusCode(), - $response->getBody() + $body ); }