在 REST API 中,DELETE 方法可以有参数吗? [英] In REST API, can DELETE methods have parameters?

查看:62
本文介绍了在 REST API 中,DELETE 方法可以有参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在REST API中,当需要参数来确定需要删除哪些资源时,我们如何创建DELETE方法?

In REST API, how do we create DELETE methods when parameters are required to determine what resources need to be deleted?

例如,照片可以属于用户和群组,如果我们有照片的端点,我们将需要额外的信息来确定我们是要删除用户照片还是群组照片,例如,

For examples, photos can belong to both users and groups, and if we have an endpoint for photos, we will need additional information to figure out if we want to delete user photos or group photos, for example,

 /photos?userId={userId}
 /photos?groupId={groupId}

这是一个好的 Restful 做法吗?

is this a good Restful practice?

或者,DELETE 是否应该严格仅通过 users/:id/photo 或 groups/:id/photo 端点发生?

Alternatively, should DELETE only happen through users/:id/photo or groups/:id/photo endpoints strictly?

推荐答案

在集合上使用 DELETE 并按查询参数过滤没有任何问题.REST 论文和 HTTP 规范都没有说明不这样做.

There's nothing wrong with using DELETE on a collection and filtering by query parameters. Neither the REST dissertation nor the HTTP spec say anything about not doing this.

这与@Thilo 链接的问题的答案不同,因为情况不同.这个问题是关于包括不,真的,删除它!"查询参数,这是不合适的.您正在使用查询参数来过滤应删除的结果.

This is different than the answer to the question that @Thilo linked to because the circumstances are different. That question was about including a "no, really, delete it!" query parameter, which is inappropriate. You're using the query parameter to filter the results which should be deleted.

这篇关于在 REST API 中,DELETE 方法可以有参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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