Following is the code I am using.
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App;
function handle_response($output, $response){
if($output['status'] === "failed"){
$response = $response->withStatus(400);
$response->getBody()->write(json_encode($output));
}
else{
$response->getBody()->write(json_encode($output));
}
return $response;
}
/* user profile */
$app->get('/user/profile', function (Request $request, Response $response) {
$headers = $request->getHeaders();
foreach ($headers as $name => $values) {
$name . ": " . implode(", ", $values);
}
if ($request->hasHeader('Authorization')) {
$output['token'] = 'yes';
}
else
$output['token'] = $name;
$output['status'] = "success";
return handle_response($output, $response);
});
I am using postman in google chrome to test this REST service. I am passing a custom header "Authorization" along with the GET request. however, this program doesn't recognize any custom headers.
The code block
$headers = $request->getHeaders();
foreach ($headers as $name => $values) {
$name . ": " . implode(", ", $values);
}
only returns the header "HTTP_ACCEPT_LANGUAGE".
Please help me to figure out, what went wrong here.