/ std@0.157.0 / media_types


Provides an API for handling media (MIME) types.

These APIs are inspired by the GoLang mime package and jshttp/mime-types, and is designed to integrate and improve the APIs from

The vendor folder contains copy of the jshttp/mime-db db.json file along with its license.


Given a extension or media type, return a fully qualified header value for setting a Content-Type or Content-Disposition header. The function will process the value passed as a media type if it contains a /, otherwise will attempt to match as an extension, with or without the leading ..

Note: a side effect of deno/x/media_types was that you could pass a file name (e.g. file.json) and it would return the content type. This behavior is intentionally not supported here. If you want to get an extension for a file name, use extname() from std/path/mod.ts to determine the extension and pass it here.

import { contentType } from "";

contentType(".json"); // `application/json; charset=UTF-8`
contentType("text/html"); // `text/html; charset=UTF-8`
contentType("txt"); // `text/plain; charset=UTF-8`
contentType("foo"); // undefined
contentType("file.json"); // undefined


Given a media type, return the most relevant extension. If no extension can be determined undefined is returned.

import { extension } from "";

extension("text/plain"); // `txt`
extension("application/json"); // `json`
extension("text/html; charset=UTF-8"); // `html`
extension("application/foo"); // undefined


Given a media type, return an array of extensions that can be applied. If no extension can be determined undefined is returned.

import { extensionsByType } from "";

extensionsByType("application/json"); // ["js", "mjs"]
extensionsByType("text/html; charset=UTF-8"); // ["html", "htm", "shtml"]
extensionsByType("application/foo"); // undefined


Given a media type and optional parameters, return a spec compliant value. If the parameters result in a non-compliant value, an empty string ("") is returned.

import { formatMediaType } from "";

formatMediaType("text/plain", { charset: "UTF-8" }); // `text/plain; charset=UTF-8`


Given a media type, return the charset encoding for the value.

import { getCharset } from "";

getCharset("text/plain"); // `UTF-8`
getCharset("application/foo"); // undefined
getCharset("application/news-checkgroups"); // `US-ASCII`
getCharset("application/news-checkgroups; charset=UTF-8"); // `UTF-8`


Given a header value string, parse a value into a media type and any optional parameters. If the supplied value is invalid, the function will throw.

import { parseMediaType } from "";
import { assertEquals } from "";


  parseMediaType("text/html; charset=UTF-8"),
    { charset: "UTF-8" },


Given an extension, return a media type. The extension can have a leading . or not.

import { typeByExtension } from "";

typeByExtension("js"); // `application/json`
typeByExtension(".HTML"); // `text/html`
typeByExtension("foo"); // undefined
typeByExtension("file.json"); // undefined
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars

Version Info

Tagged at
a week ago