Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird Unable to parse response as JSON #268

Open
ybce opened this issue Jan 23, 2018 · 4 comments
Open

Weird Unable to parse response as JSON #268

ybce opened this issue Jan 23, 2018 · 4 comments

Comments

@ybce
Copy link

ybce commented Jan 23, 2018

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
Copy link

Dadinos commented May 10, 2018

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
Copy link

Armandov1 commented May 28, 2018

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
Copy link

Dadinos commented Jun 11, 2018

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
Copy link

jjwdesign commented Sep 27, 2018

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));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants