What I didn’t know about MIME types

Olivier Dumas
4 min readNov 20, 2020

--

Learning Ruby and Rails, I’ve always had a feeling that there were many little notions that I didn’t really understand. 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 🤓

MIME types stands for Multipurpose Internet Mail Extensions.

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 a user navigates across your website, he / she is actually making requests to your server and your server sends back the files needed through the HTTP protocol (images, json, html, javascript etc.).

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 🧬

MIME types are made with a type and a subtype.

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.

type/subtypetext/html
text/csv
application/javascript
application/json
image/jpeg
image/png
...

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 🧐

MIME types can have an optional parameter that will provide additional informations to the browser.

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 🤯

I think the MDN documentation provides a very clear explanation for both types here so I will use the definition provided :

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 👨‍💻 👩‍💻

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

In Rails, respond_to allows us to send different (or not) data according to an incoming request. For example :

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

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 know there are lots of other concepts around MIME types but that’s all for this article, feel free to improve my explanation in the comments, I might add more concepts here in the future 👀

👉 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 Numbers

What I didn’t know about Ruby Classes

--

--

Olivier Dumas
Olivier Dumas

Written by Olivier Dumas

Fullstack developer working with Nest.js, Typescript, Ruby on Rails, React and Next.js | Maker of stanza.dev

No responses yet