RESTful POSTS,您是否将对象 POST 到单数或复数 Uri?

发布于 2024-08-10 12:28:35 字数 260 浏览 4 评论 0原文

这些 URI 中哪一个更“适合”接收 POST(添加产品)?是否有任何可用的最佳实践或者只是个人偏好?

/product/(单数)

/products/(复数)

目前我们使用 /products/?query=blah 进行搜索和 /product/{productId}/ 用于 GET、PUT 和删除单个产品。

Which one of these URIs would be more 'fit' for receiving POSTs (adding product(s))? Are there any best practices available or is it just personal preference?

/product/ (singular)

or

/products/ (plural)

Currently we use /products/?query=blah for searching and /product/{productId}/ for GETs PUTs & DELETEs of a single product.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

马蹄踏│碎落叶 2024-08-17 12:28:35

由于 POST 是“追加”操作,因此 POST 到 /products可能更加英国化,因为您会将新产品追加到现有产品列表中。

只要您对 API 中的某些内容进行了标准化,我认为就足够了。

由于 REST API 应该是超文本驱动的,因此 URI 无论如何都是相对无关紧要的。客户端应该从返回的文档中提取 URI,并在后续请求中使用这些 URI;通常,应用程序和人们不需要猜测或直观地解释 URI,因为应用程序将明确指示客户端哪些资源和 URI 可用。

Since POST is an "append" operation, it might be more Englishy to POST to /products, as you'd be appending a new product to the existing list of products.

As long as you've standardized on something within your API, I think that's good enough.

Since REST APIs should be hypertext-driven, the URI is relatively inconsequential anyway. Clients should be pulling URIs from returned documents and using those in subsequent requests; typically applications and people aren't going to need to guess or visually interpret URIs, since the application will be explicitly instructing clients what resources and URIs are available.

复古式 2024-08-17 12:28:35

通常,当您事先不知道资源的标识符时,您可以使用 POST 创建资源,而当您知道时,则使用 PUT。因此,您可以 POST 到 /products,或 PUT 到 /products/{new-id}。

使用这两种方法,您将返回 201 Created,并且使用 POST 还会返回一个 Location 标头,其中包含新创建资源的 URL(假设已成功创建)。

Typically you use POST to create a resource when you don't know the identifier of the resource in advance, and PUT when you do. So you'd POST to /products, or PUT to /products/{new-id}.

With both of these you'll return 201 Created, and with the POST additionally return a Location header containing the URL of the newly created resource (assuming it was successfully created).

绮烟 2024-08-17 12:28:35

在 RESTful 设计中,有一些围绕创建新资源的模式。您选择的模式很大程度上取决于谁负责为新创建的资源选择 URL。

如果客户端负责选择 URL,则客户端应 PUT 到资源的 URL。相反,如果服务器负责资源的 URL,则客户端应 POST 到“工厂”资源。通常,工厂资源是正在创建的资源的父资源,并且通常是复数的集合。

因此,就您而言,我建议使用 /products

In RESTful design, there are a few patterns around creating new resources. The pattern that you choose largely depends on who is responsible for choosing the URL for the newly created resource.

If the client is responsible for choosing the URL, then the client should PUT to the URL for the resource. In contrast, if the server is responsible for the URL for the resource then the client should POST to a "factory" resource. Typically the factory resource is the parent resource of the resource being created and is usually a collection which is pluralized.

So, in your case I would recommend using /products

牵强ㄟ 2024-08-17 12:28:35

您发布或获取单一事物:单一产品。

有时您会在没有特定产品(或有查询条件)的情况下获取。但你还是用单数说。

你很少使用复数形式的名字。如果您有一个集合(产品目录),那么它就是一个目录。

You POST or GET a single thing: a single PRODUCT.

Sometimes you GET with no specific product (or with query criteria). But you still say it in the singular.

You rarely work plural forms of names. If you have a collection (a Catalog of products), it's one Catalog.

许你一世情深 2024-08-17 12:28:35

我只会发布到单数 /product。很容易混淆这两个 URL,从而造成混淆或犯错误。

I would only post to the singular /product. It's just too easy to mix up the two URL-s and get confused or make mistakes.

猫烠⑼条掵仅有一顆心 2024-08-17 12:28:35

正如很多人所说,你可以选择任何你喜欢的风格,只要保持一致即可,但我想指出双方的一些论点;我个人偏向于单数

支持复数资源名称:

  • URL 方案的简单性,因为您知道资源名称始终为复数,
  • 许多人认为这种约定类似于数据库表的寻址方式,并认为这是一个优势
  • 似乎更广泛采用

赞成使用单数资源名称(在处理多个资源时不排除复数),

  • URL 方案更复杂,但您可以获得更多的表达能力,
  • 当您根据资源名称处理一个或多个资源时,您总是知道,如反对检查资源是否有额外的 Id 路径组件
  • 复数对于非母语人士来说有时会更困难(当不是简单的“s”时)
  • URL 较长
  • ,“s”似乎是多余的
  • 从程序员的角度来看 将路径参数视为集合的子资源而不是考虑它的本质是很尴尬的:只是它标识的资源的 ID,
  • 您可以仅在需要的地方应用过滤参数(具有复数资源名称的端点) )

As many said, you can probably choose any style you like as long as you are consistent, however I'd like to point out some arguments on both sides; I'm personally biased towards singular

In favor of plural resource names:

  • simplicity of the URL scheme as you know the resource name is always at plural
  • many consider this convention similar to how databases tables are addressed and consider this an advantage
  • seems to be more widely adopted

In favor of singular resource names (this doesn't exclude plurals when working on multiple resources)

  • the URL scheme is more complex but you gain more expressivity
  • you always know when you are dealing with one or more resources based on the resource name, as opposed to check whether the resource has an additional Id path component
  • plural is sometimes harder for non-native speakers (when is not simply an "s")
  • the URL is longer
  • the "s" seems to be a redundant from a programmers' standpoint
  • is just awkward to consider the path parameter as a sub-resource of the collection as opposed to consider it for what it is: simply an ID of the resource it identifies
  • you can apply the filtering parameters only where they are needed (endpoint with plural resource name)
指尖上的星空 2024-08-17 12:28:35

您可以对所有这些内容使用相同的 url,并使用 MessageContext 来确定 Web 服务的调用者想要执行的操作类型。
没有指定语言,但在 Java 中你可以做这样的事情。

WebServiceContext ws_ctx;
MessageContext ctx = ws_ctx.getMessageContext();
String action = (String)ctx.get(MessageContext.HTTP_REQUEST_METHOD);
if(action.equals("GET")
  // do something
else if(action.equals("POST")
  // do something

这样您就可以检查发送到 Web 服务的请求类型,并根据请求方法执行适当的操作。

you could use the same url for all of them and use the MessageContext to determine what type of action the caller of the web service wanted to perform.
No language was specified but in Java you can do something like this.

WebServiceContext ws_ctx;
MessageContext ctx = ws_ctx.getMessageContext();
String action = (String)ctx.get(MessageContext.HTTP_REQUEST_METHOD);
if(action.equals("GET")
  // do something
else if(action.equals("POST")
  // do something

That way you can check the type of request that was sent to the web service and perform the appropriate action based upon the request method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文