HTTP Has A Type System

Building HTTP APIs means finding common ground with developers who can use any programming language they want. But in any given era of history, the lingua franca is the language that is most likely to be mispronounced. More so even than JavaScript, HTTP is the Web’s lingua franca, and it has a similar, universally misunderstood status.

One of the most important things to understand about HTTP is that it has a type system.

It’s a confusing system, because its types have several names:

These are all the same thing. They have several names because of the weird history of their adoption.

MIME stands for Multipart Internet Mail Extensions. MIME types were inherited from email. They were originally developed so you could email images, Excel spreadsheets, or whatever else you wanted.

Too many web developers have assumed from this historical context that HTTP’s type system is only useful for identifying multimedia files. But when we speak of “media types,” we’re not necessarily referring to multimedia. We are necessarily referring to hypermedia, because everything on the Web is hypermedia, including any and all API communication.

And the core value of a type system is that it simplifies the process of defining interfaces. Remember, API stands for Application Programming Interface. So, unsurprisingly, HTTP types are the best way to version an API. HTTP types simplify defining APIs in other ways as well.

For instance, many web application frameworks make you validate HTTP input at the application level. You may have to write code in a model, or even a controller, which checks HTTP input to verify that you’re creating a User with appropriate data for a User.

In a model, OK. That’s a reasonable place to put type validation, even if it’s a weird place to be validating HTTP input specifically. But in a controller? As crazy as it may sound, it happens.

But you don’t have to do any of that. Instead, use HTTP’s built-in type system, via standards like JSON Schema and libraries like JSCK. Your web server can guarantee that any incoming data matches its intended type before it ever gets to the application layer. And that means your application code has less clutter and simpler logic.