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

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

问题描述

我有一个向用户发送消息的应用程序.在发布请求中,传输一个 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天全站免登陆