accepts method

bool accepts(
  1. dynamic contentType, {
  2. bool strict = false,
})

Returns true if the client's Accept header indicates that the given contentType is considered a valid response.

You cannot provide a null contentType. If the Accept header's value is */*, this method will always return true. To ignore the wildcard (*/*), pass strict as true.

contentType can be either of the following:

  • A ContentType, in which case the Accept header will be compared against its mimeType property.
  • Any other Dart value, in which case the Accept header will be compared against the result of a toString() call.

Implementation

bool accepts(contentType, {bool strict = false}) {
  var contentTypeString = contentType is MediaType
      ? contentType.mimeType
      : contentType?.toString();

  // Change to assert
  if (contentTypeString == null) {
    _log.severe('RequestContext.accepts is null');
    throw ArgumentError(
        'RequestContext.accepts expects the `contentType` parameter to NOT be null.');
  }

  _acceptHeaderCache ??= headers?.value('accept');

  if (_acceptHeaderCache == null) {
    return true;
  } else if (strict != true && _acceptHeaderCache!.contains('*/*')) {
    return true;
  } else {
    return _acceptHeaderCache!.contains(contentTypeString);
  }
}