Reputation: 176
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
in the Httpful
library. 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.
Controller.php
Authorization::checkUser();
$request = $this->request;
$callback = $request->query->get('callback');
$url = 'http://example.com/api/get_recent_posts/?' . http_build_query(
[
'count' => 20,
'post_type' => 'schedule_show',
'page' => (int) $request->query->get('page', 1),
'order' => 'ASC',
'orderby' => 'title',
'include' => 'id,title',
'_' => (int) $request->query->get('_', time()),
]
);
/** @var \Httpful\Response $APIResponse */
$APIResponse = \Httpful\Request::get($url, 'application/json')->send();
$data = $APIResponse->body;
$response = new JsonResponse($data);
$response->setCallback($callback);
return $response;
JSON Response:
{
"status":"ok",
"count":20,
"count_total":70,
"pages":4,
"posts":[
{
"id":2473,
"title":"ACOUSTIC ROOTS"
},
{
"id":2531,
"title":"AFRIKA REVISITED"
},
{
"id":2542,
"title":"AMANECER RANCHERO"
},
{
"id":2551,
"title":"APNIVANI"
},
{
"id":2504,
"title":"APT 613 LIVE"
},
{
"id":6229,
"title":"ATMOSPHERE"
},
{
"id":2532,
"title":"BLACK ON BLACK"
},
{
"id":2550,
"title":"BOUYON RASIN"
},
{
"id":2462,
"title":"CAN-ROCK"
},
{
"id":2534,
"title":"CARIBBEAN FLAVOUR"
},
{
"id":5288,
"title":"CHUO TOP 30"
},
{
"id":6060,
"title":"CINEMASCOPE"
},
{
"id":2930,
"title":"CITY SLANG"
},
{
"id":2524,
"title":"CYPHER"
},
{
"id":2484,
"title":"D’UN EXTR\u00caME \u00c0 L’AUTRE"
},
{
"id":2478,
"title":"DEMOCKERY’S DEMISE"
},
{
"id":2438,
"title":"DEMOCRACY NOW!"
},
{
"id":2546,
"title":"ETHIOPIAN SHOW"
},
{
"id":5930,
"title":"FREESTYLE"
},
{
"id":6247,
"title":"FR\u00c9QUENCE ANTILLAISE"
}
]
}
The AJAX request:
$.ajax('/shows/select/load', {
data: {
count: 20,
post_type: 'schedule_show',
page: this.page_current,
order: 'ASC',
orderby: 'title',
include: 'id,title'
},
dataType: 'jsonp',
context: this,
success: this.loadShows
});
Am I missing something?
Edit: Method that tries to parse the body and throws the exception.
JsonHandler.php:
public function parse($body)
{
$body = $this->stripBom($body);
if (empty($body))
return null;
$parsed = json_decode($body, $this->decode_as_array);
if (is_null($parsed) && 'null' !== strtolower($body))
throw new \Exception("Unable to parse response as JSON");
return $parsed;
}
For debugging purposes I added these var_dump statements:
public function parse($body)
{
var_dump($body);
$body = $this->stripBom($body);
var_dump($body);
if (empty($body))
return null;
var_dump($this->decode_as_array);
$parsed = json_decode($body, $this->decode_as_array);
var_dump($parsed);
if (is_null($parsed) && 'null' !== strtolower($body))
throw new \Exception("Unable to parse response as JSON");
return $parsed;
}
This returned:
string(1) " "
string(1) " "
bool(false)
NULL
Upvotes: -1
Views: 1804
Reputation: 176
The problem was in Request.php
in the function send
. The method uses curl and after checking the response after curl_exec
it was returning a 301 code with an empty body. All I had to do was set a new curl option curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, true);
And it worked fine.
Upvotes: 0