You are here

protected function FormatSetter::isJsonApiRequest in JSON:API 8

Checks whether the current request is a JSON API request.

Inspects:

  • request parameters
  • request path (uses a heuristic, because e.g. language negotiation may use path prefixes)
  • 'Accept' request header value.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request.

Return value

bool Whether the current request is a JSON API request.

1 call to FormatSetter::isJsonApiRequest()
FormatSetter::handle in src/StackMiddleware/FormatSetter.php
Handles a Request to convert it to a Response.

File

src/StackMiddleware/FormatSetter.php, line 69

Class

FormatSetter
Sets the 'api_json' format on all requests to JSON API-managed routes.

Namespace

Drupal\jsonapi\StackMiddleware

Code

protected function isJsonApiRequest(Request $request) {
  $is_jsonapi_route = $request->attributes
    ->get(Routes::JSON_API_ROUTE_FLAG_KEY);

  // Check if the path indicates that the request intended to target a JSON
  // API route (but may not have because of an incorrect parameter or minor
  // typo).
  $jsonapi_route_intended = strpos($request
    ->getPathInfo(), "{$this->jsonApiBasePath}/") !== FALSE;

  // Check if the 'Accept' header includes the JSON API MIME type.
  $request_has_jsonapi_media_type = count(array_filter($request
    ->getAcceptableContentTypes(), function ($accept) {
    return strpos($accept, 'application/vnd.api+json') === 0;
  }));
  return $is_jsonapi_route || $jsonapi_route_intended && $request_has_jsonapi_media_type;
}