Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using custom json content-types a good idea

I'm designing a RESTful API and in an attempt to be descriptive and make documentation clearer I want to declare my content-type http header as follows:

Content-Type: application/vnd.mycorp.mydatatype+json

where mycorp is an identifier unique to my corporation and mydatatype is unique to each data type. An example would be:

Content-Type: application/vnd.ford.car+json

{
"manufactured_year": 2000
, "color": "blue"
, "hp": 160
, "model" "Focus"
, "type": "sedan"
}

This content-type would be required in order for a POST to be valid and would be sent as a part of a response. It seems to me like a nice way to define rules for what should be inside the payload.

I can't seem to find a good resource on whether this is a good idea or if it is even allowed by IETF standards.

So, question is: Which is more feasible, application/vnd.mycorp.mydatatype+json or just application/json?

like image 485
gardarh Avatar asked Nov 08 '12 15:11

gardarh


People also ask

What is the correct JSON Content-Type?

JSON has to be correctly interpreted by the browser to be used appropriately. text/plain was typically used for JSON, but according to IANA, the official MIME type for JSON is application/json .

What does Content-Type application JSON mean?

Content-Type. application/json. Indicates that the request body format is JSON. application/xml. Indicates that the request body format is XML.

What are content types in REST API?

Some common examples of content types are “text/plain”, “application/xml”, “text/html”, “application/json”, “image/gif”, and “image/jpeg”. Similarly, to determine what type of representation is desired on the client-side, an HTTP header ACCEPT is used.

Which header should be configured to inform the server of the HTTP Content-Type?

Accept header is used by HTTP clients to tell the server which type of content they expect/prefer as response. Content-type can be used both by clients and servers to identify the format of the data in their request (client) or response (server) and, therefore, help the other part interpret correctly the information.


2 Answers

The question is strongly related to your REST API versioning.

Content type is used to, well, define the type of the content. If you use a standard content type like

application/json

you're saying to the client that the message is in JSON format. This is enough for all the web applications which do not version their API or support only the latest version. If you are going to have clients using different versions of your API, standard content types are not enough. Consider the following scenario:

Take your example as version 1 of the message

Content-Type: application/vnd.ford.carV1+json

{
"manufactured_year": 2000
, "color": "blue"
, "hp": 160
, "model" "Focus"
, "type": "sedan"
}

At some point you decide you want represent the color using hexadecimal code. Therefore you create version 2 of the type

Content-Type: application/vnd.ford.carV2+json

{
"manufactured_year": 2000
, "color": "0000FF"
, "hp": 160
, "model" "Focus"
, "type": "sedan"
}

When the client asks for a car specifies the correct content type, which includes the version. This tells the application whether send the color as hex code or as name.

Here you are versioning the representation of a resource. An alternative to support resource representation versioning is adding the version as custom header (while keeping content-type standard)

Content-Type: application/json
Message-Version: 1.0
like image 138
OGrandeDiEnne Avatar answered Sep 18 '22 17:09

OGrandeDiEnne


It's allowed, definitely. Whether it's a good idea is another story.

My rule of thumb is that it's a primary data format that's useful across a lot of things, needs to be identified on its own, and you need to interoperate across many applications, definitely give it a media type.

However, if it's just a message in your API among many, and it's only good for one resource (or one resource "type"), just use application/json.

YMMV, of course.

like image 41
Mark Nottingham Avatar answered Sep 17 '22 17:09

Mark Nottingham