accept.media_lookup

STRINGaccept.media_lookupSTRINGrequested_media_typesSTRINGdefaultSTRINGrange_defaultsSTRINGaccept_header

Available inall 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 code examples. Examples apply VCL to real-world use cases and can be deployed as they are, or adapted for your own service. See the full list of code examples for more inspiration.

Click RUN on a sample below to provision a Fastly service, execute the code on Fastly, and see how the function behaves.

Normalize requests to increase cache efficiency

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