Edit Page

req.accepts()

Return whether this request (req) advertises that it understands the specified media type.

If none of the media types are considered acceptable, this returns false. Otherwise, it returns truthy (the media type).

Usage

#
req.accepts(mediaType);

Example

#

If a request is sent with an "Accept: application/json" header:

req.accepts('application/json');
// -> 'application/json'

req.accepts('json');
// -> 'json'

req.accepts('image/png');
// -> false

If a request is sent with an "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8" header:

req.accepts('html');
// -> 'html'

req.accepts('text/html');
// -> 'text/html'

req.accepts('json');
// -> false

Notes

#
  • The specified media type may be provided as either a MIME type string such as "application/json", or an extension name such as "json".
  • This is implemented by examining the request's "Accept" header.
  • See the accepts package for the finer details of the header-parsing algorithm used in Sails/Express.

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

Reference