In this lesson, we will set one correct header value to avoid returning web pages instead of JSON results.
As you can see in the /api/categories/1
, we see data for one category.
But what happens if we pass a missing category? We get an HTML page.
But for API, it is wrong. API expects the result as a JSON. In your client, set the headers to Accept
-> application/json
. Now Laravel knows to send the result as a JSON.
Another way is to set headers using Middleware. First, let's create a Middleware.
php artisan make:middleware AlwaysAcceptJson
Inside Middleware, we have a Request. Middleware is not just for preventing something; you can also add something...