When using Rails to build an API you'll most likely be working with JSON
or XML
to pass around data. That is because most APIs today work exclusively with these MIME
(Multi-purpose Internet Mail Extension) types, and Rails is no exception. Which may lead some to wonder: what are MIME
types and what other types does Rails work with? Since you asked...
Quickie
MIME
types are just content or media file types. They provide a standard way of organizing file types on the web. Rails manages over 20 different MIME types, and you can list them by accessing the SET
class under the Mime
module. Open up a rails console and try it out:
> Mime::SET.count
=> 23
A MIME
type has two to three parts: a type, subtype and optional parameters. The type and subtype are separated by a slash:
> Mime::SET.collect(&:to_s).first
=> ""text/html""
If there are parameters they follow a semi-colon:
"text/html; charset=UTF-8"
Why does this matter? Simply put, it is because of these standards that we can faithfully build an API and expect others to be able to use it. This is essential to making the internet work. Without such standards it would be a disaster to try to pass around content with any ease. Internet programs such as servers and browsers all have a list of MIME
types, so that they can transfer files of the same type in the same way, no matter what operating system they are working in.