Laravel 5 Middleware that Requires JSON

Mar 18, 2019 laravel php
This post is more than 18 months old. Since technology changes too rapidly, this content may be out of date (but that's not always the case). Please remember to verify any technical or programming information with the current release.

Laravel has a built in request helper called wantsJson() that determines if the request is requesting JSON with the Accept: application/json header. But, what if you want to only accept JSON responses? I set up a Laravel middleware that rejects anything that isn’t JSON.

app/Http/Middleware/RequiresJson.php
<?php
declare(strict_types=1);

namespace App\Http\Middleware;

use Closure;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;

/**
 * Class RequiresJson
 * 
 * This middleware rejects the request if it doesn't have 
 * an accept application/json header
 * 
 * @package App\Http\Middleware
 */
class RequiresJson
{
  /**
   * Handle an incoming request - reject if not application/json
   *
   * @param  \Illuminate\Http\Request  $request
   * @param  \Closure  $next
   * @param  string|null  $guard
   * @return mixed
   */
  public function handle($request, Closure $next, $guard = null)
  {
      if (!$request->wantsJson()) {
        throw new NotAcceptableHttpException(
          'Please request with HTTP header: Accept: application/json'
        );
      }
  
      return $next($request);
  }
}

When the header is not set, this middleware throws a Not Acceptable 406 HTTP error. Just add this middleware to all of your API routes.

Looking for more Laravel Tips & Tricks? Join Joel and I on the No Compromises bi-weekly podcast; around 15 minutes of thoughtful real-world advice and helpful info.
Go to All Posts