简体   繁体   English

http代码部分成功/错误

[英]http code for partual success / error

I have a service that takes a list of items to handle. 我有一项需要处理的项目列表的服务。

Each of the items is handeled one at the time in the backend, when all items is done the service returns a document containing the original list of items but with a success or failure status on each line. 每个项目在后端一次都被处理,当所有项目完成后,服务将返回一个文档,其中包含项目的原始列表,但每行的状态为成功或失败。

example: 例:

PUT - body: 放置-身体:

[
  {"item" : 1},
  {"item" : 2},
  {"item" : 3}
]

reponse - body: 回应-正文:

[
  {"item" : 1, "state" : "OK"},
  {"item" : 2, "state" : "OK"},
  {"item" : 3, "state" : "FAILED"}
]

The question is now: which return code should I use if one of the items has failed? 现在的问题是:如果其中一项失败,应该使用哪个返回码? I cannot seem to find any correct matching http status code for this, it is a failure, but then again its not, but allmost;) ? 我似乎无法为此找到任何正确的匹配http状态代码,这是一个失败,但是再一次不是,而是全部;)?

From the HTTP point of view, it's a success - I'd return 200 - the request was successfully received and processed. 从HTTP的角度来看,这是成功的-我将返回200-请求已成功接收和处理。

It's then up to the app to check if it's all OK. 然后由应用程序检查是否一切正常。

However, to make life easier I might add an error field into the response? 但是,为了使生活更轻松,我可以在响应中添加一个错误字段?

A better idea would be to use a 207 multi-status response, but to send the data in a POST. 更好的主意是使用207多状态响应,但要在POST中发送数据。

This happens a lot with things that upload/update a lot of items. 上载/更新很多项目的事情经常发生这种情况。 In the multistatus response, you can correspond each action and a specific response, like so: 在多状态响应中,您可以将每个操作和特定响应对应起来,如下所示:

<response>
  <item id="1">
    <status>200 OK</status>
  </item>
  <item id="2">
    <status>403 Forbidden</status>
  </item>
</response>

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

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