Laravel - Requests - Content Negotiation

Laravel provides several methods for inspecting the incoming request's requested content types via the Accept header. First, the getAcceptableContentTypes method will return an array containing all of the content types accepted by the request:

    
    $contentTypes = $request->getAcceptableContentTypes();
	

The accepts method accepts an array of content types and returns true if any of the content types are accepted by the request. Otherwise, false will be returned:

    
    if ($request->accepts(['text/html', 'application/json'])) {
        // ...
    }
	

You may use the prefers method to determine which content type out of a given array of content types is most preferred by the request. If none of the provided content types are accepted by the request, null will be returned:

    
    $preferred = $request->prefers(['text/html', 'application/json']);
	

Since many applications only serve HTML or JSON, you may use the expectsJson method to quickly determine if the incoming request expects a JSON response:

    
    if ($request->expectsJson()) {
        // ...
    }