nategood / httpful

A Chainable, REST Friendly, PHP HTTP Client. A sane alternative to cURL.
MIT License
1.74k stars 298 forks source link

Weird `Unable to parse response as JSON` #268

Open ybce opened 6 years ago

ybce commented 6 years ago

I have a controller that sends a get request and then tries to parse the response as a JSON. However, requesting it through the browser returns a valid JSON object. I'm getting a 500 error that says 'Exception: Unable to parse response as JSON' and points to JsonHandler.php. I looked through the file and it is because the body is empty. I am confused since the request url is valid and returns a valid JSON object.

I'm using $APIResponse = \Httpful\Request::get($url, 'application/json')->send(); in the controller, however this never returns anything since it gets blocked when it hits JsonHandler.php. I feel like somewhere between the request is sent and the JsonHandler, the body is being malformed?

Dadinos commented 6 years ago

I solved it like this:

try { $doctypes = \Httpful\Request::get($url) ->authenticateWith('demo', 'demo') ->autoParse(false) ->send(); } catch (\Exception $e) { echo "API server returned an error"; echo $e->getMessage(); die(); }

    if($doctypes->code == 200) {
        // no error parse the returned string
        $doctypes = json_decode($doctypes, true);
    } else {
        echo "<h1>&nbsp;&nbsp;<i class='fa fa-warning' ></i> API server not available</h1>";
        die();
    }
Armandov1 commented 6 years ago

Hello, @Dadinos Where does that code go? I have this error with wamp server

Fatal error: Uncaught Exception: Unable to parse response as JSON in C:\wamp64\alexya\vendor\nategood\httpful\src\Httpful\Handlers\JsonHandler.php on line 30

Dadinos commented 6 years ago

Something like this:


try {
 $APIResponse = \Httpful\Request::get($url)
 -> 'application/json')
 ->autoParse(false)
 ->send();
}
catch (\Exception $e) {
echo "API server returned an error";
echo $e->getMessage();
die();
}

 if($APIResponse ->code == 200) {
        // no error parse the returned string
        $doctypes = json_decode($doctypes, true);
    } else {
        echo "<h1>&nbsp;&nbsp;<i class='fa fa-warning' ></i> API server not available</h1>";
        die();
    }

?>
jjwdesign commented 5 years ago

I know this may be an old thread, but I thought I'd suggest something. If your request fails to give you properly formed json data, you could simply make a second request with ->autoParse(false) and then inspect that result text for error messages. The example code below was for Symfony 4.

try {
    $this->result = \Httpful\Request::get($url)
        ->expectsJson()
        ->addHeaders($headers)
        ->sendsJson()
        ->send();
} catch (\Exception $e) {
    // Try to catch non-JSON formed server issues and errors
    $this->result = \Httpful\Request::get($url)
        ->expectsJson()
        ->addHeaders($headers)
        ->autoParse(false)
        ->send();
    $msg = $e->getMessage();
    $this->logger->error(__METHOD__ . ' - ' . __LINE__ . ': Httpful request error: ' .
        print_r($msg, true));
}