I have a route that calls a third-party API and returns its response. This route can take an ID as a parameter like so: /my/route/{id}
.
When I request /my/route/1
I get a 200 success, but when I do /my/route/2
, /my/route/3
, /my/route/4
, etc I get 500 error.
The funny thing is that I always get the correct response body. So both the 200 and 500 responses are returning the data that I need.
My issue is when getSponsor(..)
is triggered here:
<?php
namespace App\Http\Controllers\Matrix;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class SponsorReadingController extends Controller
{
/**
* SponsorReadingController constructor.
*/
public function __construct()
{
$this->cookieJar = app('MatrixCookieJar');
$this->client = app('GuzzleClientForMatrix');
}
public function getSponsor($sponsorId, Request $request)
{
// TODO: Refactor this monstrous function
if (!AuthController::isAlreadyLoggedIn($request)) {
$loginRes = AuthController::loginToMatrixApi($this->cookieJar);
if ($loginRes->getStatusCode() === 200) {
$sessionId = AuthController::getSessionIdFromResponse($loginRes);
} else {
return $loginRes;
}
} else {
$sessionId = null;
AuthController::setAuthCookie(
$this->cookieJar, $request->cookie('matrix_api_session')
);
}
$respData = [
'error' => null,
'message' => null,
'data' => json_decode(
$this->client->get(
'/admin/sponsor/detail',
[
'query' => ['sponsorId' => $sponsorId],
'cookies' => $this->cookieJar,
'allow_redirects' => false
]
)->getBody())->response
];
return $this->handleResponse($respData, $sessionId);
}
/**
* Handle the response with the provided data and
* cookie value.
*
* @param array $respData
* @param string $cookieVal
* @return Response
*/
public function handleResponse($respData, $cookieVal)
{
if (!empty($cookieVal)) {
return response()->json($respData)->withCookie(
cookie('matrix_api_session', $cookieVal, 29, '/matrix/api')
);
}
return response()->json($respData);
}
EDIT: If I do dd($res)
instead of return $res
inside handleResponse(...)
I get a 200 status code, weird.