mime

mime media-type parsing and formatting, and how ParseMediaType differs from content sniffing.

モジュールページの本文は現在英語です。

The mime package parses and formats media types, the strings used in Content-Type and Content-Disposition headers. Import it as "mime". ParseMediaType reads that string and its parameters. It does not look at a file or a request body, and it is not a content sniffer.

http.DetectContentType, in net/http, implements the MIME sniffing algorithm on at most the first 512 bytes of data. That is a different function in a different package. TypeByExtension looks up an extension in a built-in table and, on many systems, a local MIME database or registry, so this page does not treat its result as a portable constant.

Outputs were recorded with Go 1.24.0.

Examples were checked against Go 1.24.0. See the official package documentation for the complete API.

Go reference overview →

API index

APISummary
mime.ParseMediaType and mime.FormatMediaTypeParse a type with charset and a quoted parameter, format a type, and show an invalid parameter error.
http.DetectContentType contrasted with mime.ParseMediaTypeSniff two in-memory buffers. This is net/http, not mime, and it does not parse a Content-Type header.

ParseMediaType and FormatMediaType

ParseMediaType returns the type in lowercase, a non-nil parameter map, and an error. Official documentation says parameter names are lowercased and values keep their case, and that a parameter error returns the media type together with ErrInvalidMediaParameter. FormatMediaType writes a type and a parameter map; it returns an empty string if the arguments would violate the production rules.

mime.ParseMediaType and mime.FormatMediaType

func ParseMediaType(v string) (mediatype string, params map[string]string, err error)
func FormatMediaType(t string, param map[string]string) string

Parse a type with charset and a quoted parameter, format a type, and show an invalid parameter error.

ParameterDescription
vA media type string, including optional parameters, such as a Content-Type header value.
t, paramThe type and parameter map given to FormatMediaType. Names are written in lowercase.

Returns: ParseMediaType returns the media type, a parameter map, and an error. FormatMediaType returns the serialized string, or empty on a standard violation.

Errors

  • ErrInvalidMediaParameter — Returned when the type is present but a parameter cannot be parsed, as in text/plain; charset without a value. The example prints the error string.
  • The quoted parameter foo="Bar" is returned with the value Bar, case preserved, under the lowercase key foo.
go
package main

import (
	"fmt"
	"mime"
)

func main() {
	media, params, err := mime.ParseMediaType(`text/plain; charset=utf-8; foo="Bar"`)
	fmt.Println(media, params["charset"], params["foo"], err)
	fmt.Println(mime.FormatMediaType("text/plain", map[string]string{"charset": "utf-8"}))
	_, _, err = mime.ParseMediaType("text/plain; charset")
	fmt.Println(err)
}

Expected output

text/plain utf-8 Bar <nil>
text/plain; charset=utf-8
mime: invalid media parameter

Related: mime/multipart

Official documentation →

Parsing a header is not sniffing a body

ParseMediaType only interprets the header string you give it. http.DetectContentType looks at raw bytes and always returns some MIME type, defaulting to application/octet-stream. The example runs DetectContentType on two in-memory buffers so the difference is visible. It does not open files, and it does not replace ParseMediaType for reading Content-Type.

http.DetectContentType contrasted with mime.ParseMediaType

func DetectContentType(data []byte) string

Sniff two in-memory buffers. This is net/http, not mime, and it does not parse a Content-Type header.

ParameterDescription
dataAt most the first 512 bytes are considered. The slices here are short literals, not files or network bodies.

Returns: A MIME type string. For these two buffers on Go 1.24.0 the results are text/plain; charset=utf-8 and text/html; charset=utf-8.

  • TypeByExtension is also not a sniffer; it is a table lookup that may consult the local system, so it is described in the introduction rather than asserted here.
go
package main

import (
	"fmt"
	"net/http"
)

func main() {
	fmt.Println(http.DetectContentType([]byte("hello")))
	fmt.Println(http.DetectContentType([]byte("<html><body>x</body></html>")))
}

Expected output

text/plain; charset=utf-8
text/html; charset=utf-8

Related: net/http (server), Official DetectContentType

Official documentation →