M
M
Maxim2021-09-16 12:48:49
API
Maxim, 2021-09-16 12:48:49

Laravel middlaware auth:api returns HTML, why?

Good afternoon.

Please help. I tried most of the methods from the Internet, but they do not work for me.
There is API authorization through laravel passport. The registration and authorization methods work correctly and return a token.

But there is a problem with protected routes middlaware auth:api
Route of the following type works, and returns JSON data format:

Route::middleware('auth:api')->get('/user', function (Request $request) {
      return $request->user();
  });

But if you use the following structure:
Route::post('/auth/logout', [RegisterController::class, 'logout'])->middleware('auth:api');

Then I get a response in the form of an html page.
614311e914bbe864628619.png
If I pass the wrong token, then I get normal data already:
6143122696233246494799.png
If at the same time I use a method in Route that is not in the controller, then nothing will change, the answer will be the same. As if this part is not executed and the controller does not start.

I suspect that I'm missing something and doing it wrong. Please point me to this

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kovalchuk, 2021-09-20
@Vanhelm


Content-Type : application/json
It is necessary to put in postman instead
Accept : application/json
Content-Type- indicates what type of content the client will send
Accept- indicates what type of content it expects from the server
Therefore, laravel perceives the client as a browser and returns html and not json

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question