3

this is what I am doing

I make a first call to get the token

$client->request('POST', '/api/login_check', [], [],
        ['CONTENT_TYPE' => 'application/json'],
        json_encode(
            [
                "username" => $user->getUsername(),
                "password" => 'password',
            ]
        )
    );

    $response = $client->getResponse();
    return json_decode($response->getContent())->token;

then a second one to use it

        $client->request('GET', '/api/my_endpoint', [], [], [
            'headers' => [
                'Authorization' => $token
            ]
        ]);

$token is a valid token (tested using postman) like 'Bearer SUPERLONGSTRING' but I am getting the error message

JWT Token not found

thanks

user3174311
  • 1,714
  • 5
  • 28
  • 66

3 Answers3

4
 $client->request('GET', '/api/my_endpoint', [], [], [
                 'HTTP_AUTHORIZATION' => "{$token}",
                 'CONTENT_TYPE' => 'application/ld+json',
                 'HTTP_ACCEPT' => 'application/ld+json'
        ]);

You should use the HTTP_AUTHORIZATION header for that. Try above code. Also you don't need a nested array for headers.

Also since we don't see the format of your token keep in mind that the bearer format is:

Bearer (space) the rest of the token.

pr1nc3
  • 8,108
  • 3
  • 23
  • 36
  • sorry @pr1nc3 this is still returning me {"code":401,"message":"JWT Token not found"} – user3174311 Apr 08 '19 at 09:59
  • Yes my bad, try again fixed my answer. – pr1nc3 Apr 08 '19 at 10:02
  • Still no luck. I am redirected to the main API page with HTTP 200 status. I extended the question to provide more context. Do I need to specify I am expecting a JSON response? – user3174311 Apr 08 '19 at 10:17
  • Oh yes you have to i will edit my answer to include that as well. You can check my new answer. – pr1nc3 Apr 08 '19 at 10:34
  • Sorry, still redirecting. this is a phpunit test so maybe can be some config option? Using postman it works as expected. – user3174311 Apr 08 '19 at 10:43
  • This should work for php unit test in symfony weird. Are you sure that $token variable holds the token? Do you assign any value to it cause based on your code you dont. – pr1nc3 Apr 08 '19 at 10:52
  • yes the tokes is something like that: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE1NTQ3MjEwMjAsImV4cCI6MTU1NDcyNDYyMSwicm9sZXMiOlsiUk9MRV9VU0VSIl0sInVzZXJuYW1lIjoidXNlcjEifQ.WdRRySkB-9HxgXPY3__4ues2LHZd9DKWcZEZ1CTtnLkCvoNlfD3biUSPV3B3bIpUHyiLpZs2e1_kw0qyrCfVrss6reEC1UzISW3ATWIlUkqq7RUALqixEAO_E-CEUIQMDkZY9kJR6eAoW4-Ddv7s6a7UMMnFPsjYv-EZke_doQr9tMs5qhMhR9vAREsXUS7vaHuiUHiF2X4p1xqJXi4trpzr15GAwgPKNrJNNITQksPyrEoJsbXthqQl6m9jeiyvxwx4lC1d3LFN9AkaRWrp2nwfUXW8D7jvmXnWBbctJZBURiP5chrCNvxQTLnJO7TFYeTvKfBeXupImQ-5OT2OUS01epiYmtqCqYW_z53D1yHQMu5nFTv4ay5mWYKqrEejKh-3Y5ti_I5463BWzLYwKJYJ2nKvz- – user3174311 Apr 08 '19 at 10:58
  • I made another small change not sure that this could be the case but give it another try, Mind the double quotes. – pr1nc3 Apr 08 '19 at 11:01
  • so it looks like we need to add 'HTTP_AUTHORIZATION' => $token, 'CONTENT_TYPE' => 'application/ld+json', 'HTTP_ACCEPT' => 'application/ld+json' if you update the answer I'll accept it. – user3174311 Apr 08 '19 at 16:57
  • Hello I have exactly the same issue except that this solution does not works... I still have a crazy behaviour... Igot always "token_invalid" for my JWT token. But when I debug and I take the token and I test with postman the token generated and analyzed by the JWT bundle, it works. The token is actually valid but detected as invalid un test env... Idon't knowwhat to do, I'm stuck on that since hours and hours... any clue? – David Vander Elst Jan 28 '21 at 21:06
1
    $client->request('GET', '/api/my_endpoint', [], [], [
        'headers' => [
            'Authorization' => 'bearer '.$token
        ]
    ]);
Abdelhak ouaddi
  • 474
  • 2
  • 4
0

This is how to send a bearer token for example

$client->request('GET', '/api/my_endpoint', [
        'auth_bearer' => $token",
    ]);
Retch
  • 93
  • 1
  • 1
  • 5