#Getting rid of default views/changing default Content Type

1 messages · Page 1 of 1 (latest)

oak sparrow
#

Starting from a new Laravel 11 project, attempting to make a simple REST API Server, I'm trying to remove the default content being returned when calling routes and them (for example) returning an error (see attachment)

In this case I get the error because the Accept header is not set; If it were set I'd be getting the correct response from the API I'm calling. Is there a way to remove these default responses and/or change the default response to be application/json?

oak sparrow
#

I've found it's possible with middleware, but it seems like a "half" solution:

// app/Http/Middleware/JSONResponse.php
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class JSONResponse
{
    public function handle(Request $request, Closure $next)
    {
        $request->headers->set('Accept', 'application/json');
        $request->headers->set('Content-Type', 'application/json');

        return $next($request);
    }
}
// bootstrap/app.php
...
->withMiddleware(function (Middleware $middleware) {
        //region App
        $middleware->append(JSONResponse::class);
        //endregion
...

Any suggestions to make this a bit more clean would be much appreciated 😄