accept.media_lookup

STRINGaccept.media_lookupSTRINGrequested_media_typesSTRINGdefaultSTRINGrange_defaultsSTRINGaccept_header

Available in all subroutines.

Selects the best match from a string in the format of an Accept header's value in the listed media types using the algorithm described in Section 5.3.2 of RFC 7231.

This function takes the following parameters:

  1. a colon-separated list of media types available for the resource,
  2. a fallback return value,
  3. a colon-separated list of media types, each corresponding to a media type pattern,
  4. a string representing an Accept header's value.

The matching procedure is case insensitive and matched media types are returned verbatim as supplied to the matching function. Values of the first three arguments cannot contain variables. Duplicate media types are not allowed among the first three arguments.

Example

# We wish `image/jpeg` to return `image/jpeg`.
# We wish `image/png` to return `image/png`.
# We wish `image/*` to return `image/tiff`.
# We wish `text/*` to return `text/html`.
# We wish `*/*` to return `text/plain`.
set beresp.http.media = accept.media_lookup("image/jpeg:image/png",
"text/plain",
"image/tiff:text/html",
req.http.Accept);

Try it out

accept.media_lookup is used in the following solution recipes, which show real world use cases. Click RUN to provision a Fastly service, execute the code on Fastly, and see how the function behaves.

Normalize requests

Improve cache performance by normalizing requests. Filter and reorder query params, convert to lowercase, filter headers, and more.

User contributed notes

We welcome comments that add use cases, ideas, tips, and caveats. All comments will be moderated before publication. To post support questions, visit our support center and we'll find you the help you need.