REST - Query string vs. URL path

倾然丶 夕夏残阳落幕 提交于 2021-01-25 07:30:47

问题


I am working on a Web Api application and I am currently struggling with my understanding of RESTful API Design.

Let's say there is a Post resource /api/posts and I want the clients give the opportunity to request all Posts for the current month. From my understanding there are 2 ways to achieve this.

One way could be to make the current month as a resource. The other way could be a query string for the Post resource.

What is the best (RESTful) way to do this?

api/posts/currentmonth

or

api/posts?stardate=???&enddate=???


回答1:


What is the best (RESTful) way to do this?

REST doesn't care about URI design.

URI design guidelines intend primarily to make things easier for humans; either by making the spellings easier to reason about, or by making routing implementations easier to work with.

Alan Bates answered this very thoroughly on softwareengineering. Summarizing: RFC 3986 tells us that hierarchy belongs in the path, non-hierarchical data belongs in the query.

One way could be to make the current month as a resource. The other way could be a query string for the Post resource.

Careful...

api/posts
api/posts?stardate=???&enddate=???

Those are different resources, as far as REST and your RESTful clients are concerned. The query string is part of the identifier.

Saying the same thing a different way

GET /api/posts?stardate=???&enddate=??? ...

means

Resource(/api/posts?stardate=???&enddate=???).get()

not

Resource(/api/posts).get(stardate=???&enddate=???)
Resource(/api/posts).query(stardate=???&enddate=???).get()

Your routing framework may translate the former into the one of the latter, but that's an implementation detail. REST is designed to separate these details from the interface, so that the clients and servers can evolve independently.

Why are api/posts and api/posts?stardate=???&enddate=??? different resources? I always thought that query strings are just a way to query on a resource.

My understanding is that view is somewhat dated.

RFC 1630 does include a definition that aligns with that interpretation

The question mark ("?", ASCII 3F hex) is used to delimit the boundary between the URI of a queryable object, and a set of words used to express a query on that object. When this form is used, the combined URI stands for the object which results from the query being applied to the original object.

However, the standard language was changed by the time we got to RFC 3986

The path component contains data, usually organized in hierarchical form, that, along with data in the non-hierarchical query component (Section 3.4), serves to identify a resource within the scope of the URI's scheme and naming authority (if any).

emphasis added

A key feature of resources, as described by Fielding, is that they are abstract.

REST uses a resource identifier to identify the particular resource involved in an interaction between components. REST connectors provide a generic interface for accessing and manipulating the value set of a resource, regardless of how the membership function is defined or the type of software that is handling the request.

In other words, the representation might be generated by passing a query to an object, or it might be generated by using the identifier as the key when accessing a document store -- or an entry in a cache. The client (and the intermediary components) don't need to know anything about it implementation hidden behind the interface; they just identify the conceptual mapping they are interested in, and let the server do its thing.



来源:https://stackoverflow.com/questions/43819583/rest-query-string-vs-url-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!