diff --git a/src/Httpful/Handlers/JsonHandler.php b/src/Httpful/Handlers/JsonHandler.php index 95baf3e..af6cf76 100644 --- a/src/Httpful/Handlers/JsonHandler.php +++ b/src/Httpful/Handlers/JsonHandler.php @@ -6,7 +6,7 @@ namespace Httpful\Handlers; -class JsonHandler extends MimeHandlerAdapter +class JsonHandler extends MimeHandlerAdapter { /** * @param string $body @@ -14,12 +14,14 @@ class JsonHandler extends MimeHandlerAdapter */ public function parse($body) { + if (empty($body)) + return ""; $parsed = json_decode($body, false); if (is_null($parsed)) throw new \Exception("Unable to parse response as JSON"); return $parsed; } - + /** * @param mixed $payload * @return string diff --git a/tests/Httpful/HttpfulTest.php b/tests/Httpful/HttpfulTest.php index 2daefcf..22b7fbb 100644 --- a/tests/Httpful/HttpfulTest.php +++ b/tests/Httpful/HttpfulTest.php @@ -176,6 +176,13 @@ function testJsonResponseParse() $this->assertEquals(1, $response->body->array[0]); } + function testEmptyJsonResponseParse() + { + $req = Request::init()->sendsAndExpects(Mime::JSON); + $response = new Response("", self::SAMPLE_JSON_HEADER, $req); + $this->assertEquals("", $response->body); + } + function testXMLResponseParse() { $req = Request::init()->sendsAndExpects(Mime::XML);