REST:帖子与帖子以及新建与创建

Zad*_*org 5 rest url

当您为应用程序设计 URL 模式时,您使用哪些规则?

  • example.com/post/example.com/posts/查看帖子列表
  • example.com/post/new/example.com/post/create/用于新帖子
  • example.com/posts/ + example.com/post/23/example.com/post/ + example.com/post/23/获取帖子列表和详细信息?
  • example.com/post/23example.com/post/23/了解详细信息
  • example.com/post/edit/23/example.com/post/23/edit/用于编辑。

我更喜欢:/post/用于列表,/post/23/用于详细信息,/post/23/edit/用于编辑,只是因为我可以在浏览器位置栏中手动轻松使用该 URL。我错了?请推荐我。

谢谢。

lab*_*att 4

创建新资源时,您可能应该使用 HTTP POST 方法。因此,对于新客户,您可以发布到 example.com/customer。然后,如果您需要有关该客户的信息,请对 example.com/customer/{您最近创建的客户 ID} 执行 GET。如果您想要所有客户,请执行 GET 到 example.com/customer 如果您想编辑客户,您可能需要 PUT 到 example.com/customer/{your customer id}

看来您的根本问题是您正在处理的是您在 URL 中指定您的操作(或动词)。你不需要这样做。您应该对 example.com/23(或 example.com/customers/23)使用 HTTP PUT 方法,而不是执行类似 example.com/edit/23 的操作。

查看什么是 RESTful/REST,以回顾如何创建 RESTful 资源。

查看REST 中的 PUT 与 POST,了解 POST 和 PUT(编辑和创建)之间的区别。

为了构建更复杂的 RESTful URL,我通常会参考LinkedIn 书呆子们的演示。