All of my API responses come with HTML tags, how can I remove them?
Here is my code
Route::get('test',function() { return response("hello world"); });
I tried checking and disabling all middlewares.
2
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
Move your Route into routes/api.php file instead of routes/web.php.
Route
routes/api.php
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.
/api/test
/api
Click here to cancel reply.
2
Answers
Use json method to do it:
You can learn more about https://laravel.com/docs/10.x/responses#other-response-types
Move your
Route
intoroutes/api.php
file instead ofroutes/web.php
.And return data as json type:
You can test this by url
/api/test
, Laravel will auto prefix/api
in all routes ofroutes/api.php
as default.