关于 REST PUT 动词和其他非基于 CRUD 的操作的一些说明
我已阅读此关于休息的很好的答案。我对此有几个问题:
- 根据答案和我读到的内容,需要使用 PUT 动词创建新资源,但在大多数情况下,当您创建资源时,您需要提供参数,有时还需要提供二进制数据。 用 PUT 的方法是什么?使用多部分 POST 来完成不是更自然吗?
- 有人可以指导我使用 PUT 发送二进制文件和字符串的示例吗?最好使用 httpClient 库。
- 如果我想要超出基本 CRUD 操作的操作,应使用什么动词?就像生成有关其中一种资源的报告一样。
谢谢
I have read this GREAT answer regarding REST. I have few questions regarding it:
- According to the answer and what i read, new resource creation needs happen using the PUT verb but in most cases when you create a resource you need to provide parameters and sometimes binary data.
what is the way to do it with PUT? isn't it more natural to do it with multipart POST? - Can someone direct me to an example for use of PUT to send both binaries and String, preferably using the httpClient library.
- What verb to use if i want something that is beyond the basic CRUD actions? like generate a report regarding one of the resources.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将 HTTP 动词与实际负载混淆了。
没有什么可以阻止您使用多部分有效负载进行 PUT。
POST 可用于创建新资源,但您通常会 POST 到不同的 URL。
作为副作用,可能会创建新资源并在 Location 标头中返回。
如果您已经拥有资源的 URL,则使用 PUT。
休息!=增删改查。
宁静的架构迫使你思考资源。
因此,“报告”可能是一个很好的资源候选者。
您可以将报告参数发布到 /..../reports(例如)之类的资源,并以这种方式创建新的报告资源。将报告的 URL 放入所述 Location 标头中,并使用 GET 实际获取报告数据(或立即返回报告数据)
You are confusing the HTTP Verb with the actual payload.
Nothing prevents you from using a multipart payload for PUT.
POST can be used to create new resources, but you are usually POSTing to a different URL.
As a side-effect, a new resource might get created and returned in the Location header.
PUT is used if you already have the URL to a resource.
REST != CRUD.
A restful architecture forces you to think in resources.
So 'report' might be a good candidate for a resource.
You could POST your report parameters to a resource like /..../reports (for example) and create a new report resource that way. Put the URL for the report in said Location header and use a GET to actually get the report data (or return the report data right away)