Laravel Request: isJson()

Asked: 20 days ago


This function returns true if request has a header CONTENT_TYPE and values contains /json or +json. isJson() determines if the current request is sending JSON.


public function isJson():bool


  • If you want to check if request has Accept header set to application/json
  • This is useful if you are building rest api app where you want to check if request is json type

Example-1: Use of isJson in Laravel Controller


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
    public function index(Request $request)
        dd($request->isJson()); // output:- false

Example-2: Use of isJson function using request()

Following example can be used anywhere in Laravel app:

namespace App\Helpers;

use Illuminate\Http\Request;

class Test 
    public function __construct() 
        dd(request()->isJson()); // output:- false
Viewed: 46 times
Liked: 1 times