Hi,
I am building an app with separated API and front-end. The routes for the front-end make calls to the routes for the API using subRequest. Example below. I want to use an HTTP header response from the API and consume those values on the front-end. When I return the response withHeader and set a key and value, that key and value isn’t present to the route making the subRequest. I am trying to figure out why that is and how to fix it.
Front-end Route(/login)
$app->post('/login', function ($request, $response, $args) use($app) {
//Do some things here that aren't relevant.
$username = "user";
$password = "pass";
$result = $app->subRequest('POST', 'api/v1/login', 'username=' . $username . '&password=' . $password . '');
$headers = $response->getHeaders();
foreach($headers as $name => $values) {
echo $name . ": " . implode(", ", $values);
}
//The output from this is: Content-Type: text/html; charset=UTF-8
)};
API Route(/api/v1/login)
$this->post('/login', function ($request, $response, $args) {
$username = $request->getParam('username');
$password = $request->getParam('password');
//We're going to authenticate the user here. No need to show this code.
//Now we're going to respond to the front-end with the authentication response
if($result->isValid()) {
//We're going to do some stuff if the user is valid. In this case, we don't care about this. We only care if it isn't valid.
} else {
return $response->withHeader('valid', 'false');
}
});
As you can see from the above two examples, we’re collecting and sending the username and password from the front-end using subRequest and waiting for a response. The part that is failing is when the users details isn’t valid, I want to respond with a header key=>value and do something with the result on the front-end. On the API route, I am setting and returning the response with the header info, but on the front-end, the key=>value is missing. How can I correct this?
Thank you!