liveBook · Manning

MIME type

As explained in Go in Practice, Second Edition

A MIME type is a media-type identifier that describes the format of a file or other data, such as image/png or application/octet-stream.

A MIME type, also called a content type, identifies the format of a file or HTTP payload. Common values include image/png, application/octet-stream, and application/json. In Go, MIME types help validate uploads, reject unexpected responses, and select API representations. section 10.2.5 section 11.2.2

Validate an uploaded file using progressively more trustworthy signals:

  • Read the request's Content-Type header; it may provide a specific type such as image/png or the generic application/octet-stream. section 10.2.5
  • Inspect the filename extension and map it with mime.TypeByExtension. section 10.2.5
  • Read initial bytes and call http.DetectContentType to infer the type from file contents. section 10.2.5
  • Treat headers and extensions as untrusted hints; content parsing is harder and more resource-intensive, but more trusted. section 10.2.5
file, header, err := r.FormFile("file")
contentType := header.Header["Content-Type"][0]

Extracts the uploaded Content-Type from multipart.FileHeader. section 10.2.5

file, header, err := r.FormFile("file")
extension := filepath.Ext(header.Filename)
type := mime.TypeByExtension(extension)

Maps an uploaded filename extension to a MIME type with the standard library. section 10.2.5

file, header, err := r.FormFile("file")
buffer := make([]byte, 512)
_, err = file.Read(buffer)
filetype := http.DetectContentType(buffer)

Reads 512 bytes and detects the content type from the file's data. section 10.2.5

HTTP clients can also use MIME types to interpret errors and negotiate API versions. Check Content-Type alongside the status code before decoding an error response, and use versioned vendor types such as application/vnd.mytodos.json; version=2.0 in Accept and response headers. section 11.2.2 section 11.4.2

How Go in Practice, Second Edition covers it

How deep it goes
Compares header, extension, and content inspection approaches, then demonstrates Go HTTP handling without detailing MIME registration.
Examples
Uses image/png, application/octet-stream, extension mapping, byte inspection, JSON errors, and versioned vendor media types.
What it assumes
Assumes basic Go HTTP requests and multipart uploads while establishing MIME types through concrete standard-library operations.
What you can do after
Diagnose and implement file-type checks and HTTP representation handling with Go's multipart, mime, and net/http APIs.
How it connects
Connects MIME types with HTTP headers, multipart uploads, file extensions, content detection, error handling, and API versioning.

Also explained in

Compare how 2 books explain MIME type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.