Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

std/http/negotiation.ts>accepts

The Deno Standard Library
Go to Latest
function accepts
import { accepts } from "https://deno.land/std@0.223.0/http/negotiation.ts";

Returns an array of media types accepted by the request, in order of preference. If there are no media types supplied in the request, then any media type selector will be returned.

Examples

Example 1

import { accepts } from "https://deno.land/std@0.223.0/http/negotiation.ts";

const req = new Request("https://example.com/", {
  headers: {
    "accept":
      "text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, *\/*;q=0.8",
  },
});

console.log(accepts(req));
// [
//   "text/html",
//   "application/xhtml+xml",
//   "image/webp",
//   "application/xml",
//   "*\/*",
// ]

Returns

string[]

For a given set of media types, return the best match accepted in the request. If no media type matches, then the function returns undefined.

Examples

Example 1

import { accepts } from "https://deno.land/std@0.223.0/http/negotiation.ts";

const req = new Request("https://example.com/", {
  headers: {
    "accept":
      "text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, *\/*;q=0.8",
  },
});

accepts(req, "text/html", "image/webp"); // "text/html";

Parameters

request: Request
...types: string[]

Returns

string | undefined