expectsJson method

bool expectsJson()

True when the client wants a JSON answer, not a page: an Accept header with json, a JSON body, or an X-Requested-With: XMLHttpRequest header. An Inertia request (X-Inertia) is not.

Implementation

bool expectsJson() {
  if (header('x-inertia') != null) return false;
  String accept = header(HttpHeaders.acceptHeader) ?? '';
  if (accept.contains('/json') || accept.contains('+json')) return true;
  return isJson() || header('x-requested-with') == 'XMLHttpRequest';
}