为REST API返回部分成功响应的好方法是什么? [英] What is a good way to return a partial success response for a REST API?

查看:174
本文介绍了为REST API返回部分成功响应的好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个依赖下游API的用例.问题是,对我而言,单个资源由下游服务的多个资源组成.因此,一次写入我的API可能在内部是对外部服务的多次调用,并且这些调用中的任何一个都可能失败.

I am working on a use case where I have a dependency on a downstream API. The issue is that a single resource for me is made up of multiple resources for the downstream service. So a single write to my API may be internally multiple calls to the external service and any one of those calls may fail.

在一切正常的情况下,我将返回200 OK的空响应,在无效请求或服务器错误的情况下,我将返回单独的错误响应.如果部分成功,我应该返回什么?我知道207多状态响应代码,但是不确定在这里是否适用,因为它看起来更像批处理操作.到目前为止,带有失败子实体列表的207响应似乎是最好的选择.任何更清洁的方式的想法将不胜感激.

In the case of everything being good I am returning a 200 OK empty response and in the case of an invalid request or server error I am returning a separate error response. What should I return in the case of partial success? I am aware of the 207 Multi-status response code but am not sure if that is applicable here since that looks like it's more applicable to a batch operation. A 207 response with a list of failed sub entities looks like the best bet as of now. Any ideas of a more cleaner way would be appreciated.

推荐答案

这是一种更多的设计选择,因此最终我使用了以下响应模型 1.成功-200欧元,适用机构 2.部分成功-200正常,并带有适用的正文和警告 3. Failure(4xx/5xx)-列出错误的错误正文

This was more sort of a design choice so finally I went with the following response model 1. Success - 200 OK with applicable body 2. Partial success - 200 OK with applicable body plus warnings 3. Failure(4xx/5xx) - errors body listing the errors

这篇关于为REST API返回部分成功响应的好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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