REST - 使用单个 POST 创建嵌套资源 [英] REST - Creating Nested Resources with single POST

查看:30
本文介绍了REST - 使用单个 POST 创建嵌套资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以 RESTful 的方式思考,使用 POST 在一次调用中创建资源及其子资源是否正确?在我的应用程序中,我有资源 /notices/{notice} 和子资源 /notices/{notice}/photos/{photo}.{photo} 没有 {notice} 就不能存在,但是 {notice} 不一定有照片.通常,我必须先进行 POST 以创建通知,然后再进行 POST 以添加照片.

Thinking in a RESTful way, is it correct to use POST to create in a single call a resource and its sub-resource? In my application I have the resource /notices/{notice} and sub-resource /notices/{notice}/photos/{photo}. A {photo} can't exists without a {notice}, but a {notice} doesn't have necessarily photos. Normally, I have to do first a POST to create a notice, then another POST to add a photo.

现在我想允许创建带有直接附加照片的通知,启用/notices/{notice}/notices/{notice}/photos/{photo} 带有对/notices/{notice}/photos/{photo} 的单个 POST 请求,带有描述两种资源的多部分内容(JSON 表示通知,二进制表示照片).我想我只会为子资源返回位置标头.

Now I want to allow the creation of a notice with a photo directly attached, enabling the creation of /notices/{notice} and /notices/{notice}/photos/{photo} with a single POST request to /notices/{notice}/photos/{photo}, with a multipart content describing both the resources (JSON for notice, binary for the photo). I think I will return the Location header only for the sub-resource.

本质上,我希望这可以防止 Android 客户端向服务器发送两个 POST 请求以上传带有照片的通知.这样对吗?或者它是否违反了 REST 原则?我应该考虑将它们分开并提出两个不同的要求吗?或者将照片视为与通知分开的实体是否错误?我应该只保留 /notices/{notice} 作为资源,使用 PUT 添加照片吗?

Essentially, I want this to prevent Android clients to send two POST request to the server to upload a notice with a photo. Is this correct? Or does it infringe REST principles? Should I consider to keep them separate and make two different requests? Or is it wrong to consider photos a separate entity from the notice? Should I keep only /notices/{notice} as resource, using PUT to add photos?

哪个是最好的解决方案?

Which is the best solution?

推荐答案

是的, 在创建父资源的同时创建子资源没有任何问题.甚至可以使用 PUT 而不是 POST 来执行此操作,因为父 URL 下的所有内容都属于/属于您正在上传的资源.

Yes, there is nothing wrong with creating sub-resources at the same time you create the parent resource. It would even be OK to use PUT instead of POST to do this, as everything under the parent URL is part of/belongs to the resource you are uploading.

现在我想允许创建带有直接附加照片的通知,启用/notices/{notice}/notices/{notice}/photos/{photo} 带有一个 POST 请求到 /notices/{notice}/photos/{photo}

Now I want to allow the creation of a notice with a photo directly attached, enabling the creation of /notices/{notice} and /notices/{notice}/photos/{photo} with a single POST request to /notices/{notice}/photos/{photo}

这个我不同意.我建议发布到集合资源的 URL,/notices.您将通知及其照片作为单一表示(请求正文)提供.然后,后端将为通知和任何组成照片创建资源.

This I disagree with. I suggest POSTing to the collection resource's URL, /notices. You provide the notice and its photos as a single representation (request body). The back end will then create resources for both the notice and any constituent photographs.

这篇关于REST - 使用单个 POST 创建嵌套资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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