0

This is the RESTAdapter code as in the Ember js guide:

App.ApplicationAdapter = DS.RESTAdapter.extend({
   namespace: 'emberboot/api',

   headers: {
      'API_KEY': 'secret key'
  }
});

This is the code for getting headers from SLIM PHP. The get headers function is documented here http://docs.slimframework.com/#Request-Headers

// - http://localhost/emberboot/api/posts
// - $app->get('/posts', 'getPosts'); //get all post

function getPosts() 
{
    $request = \Slim\Slim::getInstance()->request();
    $key = $request->headers->get('API_KEY');

    //logging
    $file = 'headers.txt';
    file_put_contents($file, $key);
    ;
    ;

This is from the Chrome Developer Tool:

enter image description here

Clearly there is nothing wrong from the Ember-data part coz the header is there. However THE LOGGING FILE: headers.txt contain no text at all - empty!

UDATED - ANSWERS AS SOLVE BY @engvrdr. Use dash instead of underscore

This is the RESTAdapter code UPDATED!

App.ApplicationAdapter = DS.RESTAdapter.extend({
   namespace: 'emberboot/api',

   headers: {
      'API-KEY': 'secret key'
  }
});

The Slim PHP code updated:

function getPosts() 
{
    $request = \Slim\Slim::getInstance()->request();
    $headers = $request->headers;
    $apiKey = $headers->get('API-KEY');
    $file = 'headers.txt';
    file_put_contents($file, $apiKey);
    ;
    ;
jumper rbk
  • 414
  • 4
  • 18

1 Answers1

2

I'm not much a slim expert but can you try? (In documentation request seems like a property not a method)

function getPosts() 
{
$app = \Slim\Slim::getInstance();
$key = $app->request->headers->get('API-KEY');

//logging
$file = 'headers.txt';
file_put_contents($file, $key);

Edit : As i found out it is an issue about underscores (_) in HTTP header names (Why underscores are forbidden in HTTP header names)

if you change it to API-KEY it will work.

Community
  • 1
  • 1
engvrdr
  • 541
  • 2
  • 9