Getting "unauthorized" with x-access-token in CakePHP 4

I’m trying to perform a search of all registered products based on user_id.
In the postman, I get this list, after performing the authentication.

In the image below, you can see the return 200 in the request.

enter image description here

In my code I get 401. Can you help me analyze what I might be doing wrong?

 public function index()
        $session = $this->getRequest()->getSession();
        $http = new Client(['headers' => ['x-access-token' => 'Bearer '. $session->read('Config.TOKEN'),]]);
        $response = $http->get('http://localhost:8889/api/produto/buscaProdutosPorIdUsuario/1');

        if ($response->getStatusCode() == 401) {
            return $this->redirect($this->referer());

        if ($response->isOk()) {
            $json = $response->getJSON();
            $this->set('json', $json["data"]);

enter image description here

Why are you prefixing your token value with Bearer in the Cake Client?

You are not doing that in Postman.

1 Like

Really that was the problem. There was no “Bearer” in the header. Thanks for the feedback!

If I recall the word Bearer appears in Chrome Dev Tools network header thus indicating that string would need to be provided - I reckon I would have fallen for that too. Nice pickup Kevin.