Open ybce opened 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> <i class='fa fa-warning' ></i> API server not available</h1>";
die();
}
Hello, @Dadinos Where does that code go? I have this error with wamp server
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> <i class='fa fa-warning' ></i> API server not available</h1>";
die();
}
?>
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));
}
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 hitsJsonHandler.php
. I feel like somewhere between the request is sent and the JsonHandler, the body is being malformed?