accepts method
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
Acceptheader will be compared against itsmimeTypeproperty. - Any other Dart value, in which case the
Acceptheader will be compared against the result of atoString()call.
Implementation
bool accepts(Object? 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);
}
}