What I didn’t know about MIME types

I’ve always had this feeling that while learning Ruby and Rails, there were tons of little notions that I didn’t really understood. I’m used to write things in order to learn them so here is a series of short articles about what I didn’t know.

The basic definition 🤓

As the doc says, MIME types indicates the nature and format of a document, file, or assortment of bytes.

In other words, they are used by your web browser to know what is the type of the file it has received from the server.

MIME types are everywhere 🌞

When the server sends back the files, it also sends a header that includes the Content-type entity that looks like this

Content-Type: text/html; charset=UTF-8

Browsers use the MIME type, not the file extension, to determine how to process a URL. (source: MDN)

Every web browsers have their own list of MIME types that they supports, thus once your browser knows the MIME type sent by the server, it will match this MIME type with its own list of MIME Types and interpret your file accordingly.

MIME type composition 🧬

As you could imagine, the type represents the main category of the data type and the subtype represents the exact type of data of the MIME type.


There are more than 100 different MIME types and I am not going to list them all here but you can access to the list here.

Notice that new MIME types are created over time so this list might not be exhaustive.

MIME types optional parameter 🧐

type/subtype; parameter=valuetext/html; charset=UTF-8

As you can see above, the optional charset parameter has been passed telling the browser what character set was used for the characters in the data.

Two types of MIME types 🤯

The discrete types

Discrete types are types which represent a single file or medium, such as a single text or music file, or a single video.

The multipart types

A multipart type is one which represents a document that’s comprised of multiple component parts, each of which may have its own individual MIME type; or, a multipart type may encapsulate multiple files being sent together in one transaction. For example, multipart MIME types are used when attaching multiple files to an email.

MIME types in Ruby on Rails 👨‍💻 👩‍💻

Image for post
Image for post

Rails defines 34 MIME types in its mime_types.rb file. You can access all the types in you rails console like this :

Mime::SET.collect(&:to_s)["text/html", "text/plain", "text/javascript", "text/css", "text/calendar", "text/csv", "text/vcard", "text/vtt", "image/png", "image/jpeg", "image/gif", "image/bmp", "image/tiff", "image/svg+xml", "video/mpeg", "audio/mpeg", "audio/ogg", "audio/aac", "video/webm", "video/mp4", "font/otf", "font/ttf", "font/woff", "font/woff2", "application/xml", "application/rss+xml", "application/atom+xml", "application/x-yaml", "multipart/form-data", "application/x-www-form-urlencoded", "application/json", "application/pdf", "application/zip", "application/gzip"]

You can define your custom MIME types in the default initializer created by Rails under config/initializers/mime_types.rb .

MIME types and respond_to

respond_to do |format|
format.html { @articles = custom @articles for my html view }
format.json { @articles = custom @articles my API }

This method is actually directly linked to the default MIME types, every methods ( .html , .json …) that we can call on the format object comes from the default Rails MIME types (+ the custom MIME types loaded by the initializer mime_types.rb ).

If you want to understand deeper how respond_to works, I recommend you to read this blog post on the subject.

The last word

👉 I’am also the maker of stanza.dev that helps developers to learn coding concepts faster. Feel free to have a look 👨‍💻 👩‍💻

Cheers ✌️

Other stories I’ve written ✍️

What I didn’t know about Ruby Classes

Written by

Fullstack developer in love with Rails, React and Next.js Maker of stanza.dev

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store