Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

solshark's avatar

API 401 not triggered

Hi,

I'm new in Laravel/Spark, but want to learn it. So far I'm trying to follow tutorial. I like idea of API driven development. However, when I do API request from console using curl I expect to receive 401 without error. Instead I got html content for login page. I see many ppl complain about same in the comments area under related video tutorial.

Any hints how to solve it?

My route is:

Route::group([
    'middleware' => 'auth:api'
], function () {
    Route::get('/test', function () {
        return ['status' => 'ok'];
    });    //
});
~ ➜ curl localhost/api/test
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="refresh" content="0;url=http://localhost/login" />

        <title>Redirecting to http://localhost/login</title>
    </head>
    <body>
        Redirecting to <a href="http://localhost/login">http://localhost/login</a>.
    </body>
</html>%

And it works as expected from Vue component.

0 likes
1 reply
m1guelpiedrafita's avatar
Level 3

Laravel will return a redirect to the login page when using the auth:api driver unless:

  1. You pass the token and authenticate successfully.
  2. The request expects JSON. You can set this using the Accept header.

If both conditions fail, you will be redirected to the login page.

Please or to participate in this conversation.