部分成功请求的HTTP状态代码 [英] HTTP status code for a partial successful request

查看:519
本文介绍了部分成功请求的HTTP状态代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向用户发送消息的应用程序。在post请求中,传输的XML字符串由应该接收该特定消息的所有用户组成。如果列表中的任何用户不存在,我将缺少用户的列表返回给客户端以进行进一步评估。

I have an application that sends messages to users. In a post request a XML string is transferred that consists of all the users that should receive that particular message. If any of the users in the list do not exist I give the list of missing users back to the client for further evaluation.

现在我问自己,应用程序的正确状态代码是什么,表示请求已被接受但有些事情无法完成。

Now I'm asking myself what would be the proper status code for the application saying that the request was accepted but there were things that couldn't be done.

如果不允许在列表中包含缺少的用户,则可以避免该问题。然后发送尝试只会得到4xx错误。但是以这种方式形成API毫无意义。
另一方面,我可以认为错误条件纯粹是应用程序特定的。但发送200只是感觉不对。如果要深入了解错误响应,给客户端提供一个提示会很好。例如避免一遍又一遍地向该用户发送消息

The problem would be avoided if it weren't allowed to include missing users in the list. Then the sending attempt would just get an 4xx error. But there is no point in forming the API this way. On the other hand I could consider the error condition to be purely application specific. But sending a 200 just does not feel right. And it would be nice to give the client a hint when to look deeply in the error response. e.g. to avoid sending messages to that users over and over

推荐答案

我处理过一个非常类似的问题。在这种情况下,我返回了

I've dealt with a very similar issue. In this case, I returned a

207多状态

现在,这不是严格的HTTP,它是WebDAV扩展的一部分,所以如果你也无法控制客户端,那么这对你不利。如果你这样做,你可以这样做:

Now, this isn't strict HTTP, it's part of the WebDAV extension, so if you don't have control over the client too, then this isn't good for you. If you do, you could do something like so:

   <?xml version="1.0" encoding="utf-8" ?>
   <D:multistatus xmlns:D='DAV:'>
     <D:response>
       <D:user>user-123</D:user>
       <D:status>success</D:status>
     </D:response>
     <D:response>
       <D:user>user-789</D:user>
       <D:status>failure</D:status>
     </D:response>
   </D:multistatus>

但同样,这是一个HTTP扩展,您还需要控制客户端。

But again, this is an HTTP extension, and you need to have control of the client as well.

这篇关于部分成功请求的HTTP状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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