skip to Main Content

All of my API responses come with HTML tags, how can I remove them?

postman response

Here is my code

Route::get('test',function()
{
    return response("hello world");
});

I tried checking and disabling all middlewares.

2

Answers


  1. Use json method to do it:

    Route::get('test',function() { 
        return response()->json([
           'message' => 'Hello World',
        ]);
    });
    

    You can learn more about https://laravel.com/docs/10.x/responses#other-response-types

    Login or Signup to reply.
  2. Move your Route into routes/api.php file instead of routes/web.php.

    And return data as json type:

    # In routes/api.php
    Route::get('test',function() { 
        return response()->json(['data' => 'Hello World']);
    });
    

    You can test this by url /api/test, Laravel will auto prefix /api in all routes of routes/api.php as default.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search