REST 更新多个资源 [英] REST updating multiple resources

查看:46
本文介绍了REST 更新多个资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过搜索这个,但我没有找到适合我需要的答案.

I've tried searching this, but I haven't managed to find an answer which would fit my needs.

考虑到我目前有以下路线:

Considering I currently have the following route:

[GET] /items

哪些可以使用查询参数进行过滤.现在我需要让它能够一次添加多个资源.我已经考虑过执行以下请求:

Which can be filtered by using query parameters. Now I need to give it the ability to add multiple resources at once. I've considered of doing the following request:

[PATCH] /items

拥有这样的身体:

id[]=1&id[]=2&id[]=3&updateField=newValue

我认为这个调用有问题,但我无法弄清楚.

I think there is something wrong with this call, but i'm not able to figure it out.

推荐答案

在 RESTful API 中,URL 应该定义事务的对象,动词是动作.

In a RESTful API the URL should define the object of the transaction, and the verb the action.

所以 GET/items 应该返回所有项目.

So GET /items should return all items.

GET/items/1 应该返回 ID 为 1 的项目.

GET /items/1 should return the item with id 1.

因此,多个 id 应该是资源定义 (url) 的一部分.所以GET/items/1,2,3 应该返回 3 个合适的项目.

It follows that the multiple ids should be part of the resource definition (url). So GET /items/1,2,3 should return the 3 appropriate items.

因此,要对多个 ID 应用部分更新:

Therefore, to apply a partial update to many ids:

[PATCH] /items/1,2,3

然后在 PATCH 或 PUT 的正文中,您可以提供要更新的信息(假设您发送的是 JSON 正文).

Then within the body of the PATCH or PUT, you can provide the information to be updated (assuming you are sending a JSON body).

{"updateField": "newValue"}

这篇关于REST 更新多个资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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