简体   繁体   English

同一HTTP POST请求的多个application / json

[英]Multiple application/json for same http POST request

I am designing a REST API for incident reporting. 我正在设计用于事件报告的REST API。 There are are three state for one incident. 一个事件有三种状态。 ie investigation,resolved and postmortem. 即调查,解决和验尸。

I use uri for incident resource as 我使用uri作为事件资源

 /incident/{incident_id}/{status}

Here incident_id denotes unique id of an incident and status denotes state of an incident. 这里,identity_id表示事件的唯一ID,而status表示事件的状态。

User can create new incident(this is in investigation state) using 用户可以使用以下命令创建新事件(处于调查状态)

 POST - /incident

Say uri after creating incident is 在创建事件后说uri是

/incident/123

The applicatoin/json which is needed to be send with POST request to create resolved state and postmortem state of an incident are different. 需要与POST请求一起发送以创建事件的已解决状态和事后状态的applicatoin / json不同。

Then there are two application/json for same uri. 然后对于同一个uri,有两个application / json。

I don't need change uri to 我不需要将uri更改为

 POST -/incident/investigation/{incident_id}
 POST -/incident/resolved/{incident_id}
 POST -/incident/postmortem/{incident_id}

How to fix this problem? 如何解决这个问题?

The application/json which is needed to be send with POST request to create resolved state and postmortem state of an incident are different. 创建事件的已解决状态和事后状态需要与POST请求一起发送的application / json不同。

So there are two common answers here. 因此,这里有两个常见的答案。

One is that the POST endpoint for the resource inspects the JSON object to figure out which request was made by the client, and then does the right thing. 一种是资源的POST端点检查JSON对象,以确定客户端发出了哪个请求,然后做正确的事情。

The more mature version of this same idea is to use a different Content-Type to distinguish the json documents. 相同想法的更成熟版本是使用不同的Content-Type来区分json文档。 That is to say, if you define an application/vnd.resolved+json schema and an application/vnd.postmortem+json schema, then the client reports in the http headers which form of message it is sending to you. 也就是说,如果定义了application/vnd.resolved+json模式和application/vnd.postmortem+json模式,则客户端将在HTTP标头中报告发送给您的消息形式。

POST /incident/12345
Content-Type: application/vnd.resolved+json

{ "resolved" : { ... } }


POST /indicdent/12345
Content-Type: application/vnd.postmortem+json

{ "post-mortem" : { ... } }

RFC 6838 describes the protocols for registering new media type formats, as well as reserving several namespaces for unregistered types. RFC 6838描述了用于注册新媒体类型格式以及为未注册类型保留几个名称空间的协议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM