使用自定义json内容类型是一个好主意 [英] Is using custom json content-types a good idea

查看:380
本文介绍了使用自定义json内容类型是一个好主意的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个RESTful API并尝试描述并使文档更清晰我想要声明我的内容类型http标头如下:

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

其中mycorp是我公司独有的标识符,mydatatype对每种数据类型都是唯一的。一个例子是:

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"
}

此内容类型是必需的POST有效,将作为响应的一部分发送。在我看来,这是一个很好的方法来定义有效载荷内的规则。

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.

我似乎无法找到一个关于这是否是一个好的资源想法或者IETF标准是否允许它。

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.

所以,问题是:哪个更可行,application / vnd.mycorp.mydatatype + json或者只是application / json ?

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

推荐答案

绝对允许这样做。这是一个好主意是另一个故事。

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.

但是,如果它只是你的API中的一条消息,那么它只对一个资源(或一个资源类型),只需使用application / json。

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,当然。

这篇关于使用自定义json内容类型是一个好主意的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆