What are REST resources?

后端 未结 11 1051
青春惊慌失措
青春惊慌失措 2020-11-29 20:53

What are REST resources and how do they relate to resource names and resource representations?

I read a few articles on the subject, but they were too abstract and t

相关标签:
11条回答
  • 2020-11-29 21:42

    Representational State Transfer (REST) is a style of software architecture for distributed systems such as the World Wide Web. REST-style architectures consist of clients and servers. Clients initiate requests to servers; servers process requests and return appropriate responses. Requests and responses are built around the transfer of representations of resources. Resources are a set of addressable objects, basically files and documents, linked using URLs. As correctly pointed out above by Quentin, REST archiecture simply implies that you'd use the HTTP verbs GET/POST/PUT/DELETE...

    0 讨论(0)
  • 2020-11-29 21:45

    The URL is never a resource or its name or its representation.

    URL just tells where the resource is located and You can invoke GET,POST,PUT,DELETE etc on this URL to invoke the resource.

    Data responded back are the resources while the form of the data is its representation.

    Let's say Your URL with given GET parameters can output a JSON resource - this is the JSON representation of this resource. While with other flag in the GET it could respond with the same data in XML - that will be another representation of the very same resource.

    EDIT: Due to the comments to the OP and to my answer I'm adding another explanations.

    Also the resource name is considered to be the 'script name', e.g. in this case it is users.json while this resource name is self describing the resource representation itself - when calling this resource we expect the resource is in JSON, while when calling e.g. users.xml we would expect the data in XML.

    1. When I change the offset parameter in GET the response contains different data set - is it a new resource or its representation?
    2. When I define which columns are returned in response in GET, is it a different resource or different representation, or?
    1. Well, here the problem and answer are clear - we still call the same URL, the server responses with the data in the same form (still it is JSON), data still contains information about users - just the information itself has changed due to the new offset parameter. So it is obvious that it is still the same resource with the same representation and the same resource name as before.
    2. Second problem could be a little confusing. Though we are calling the same resource, though the resource contains the same data (just with only predefined column set) and though the data is in the same representation it could seem to us as a different resource. But due to the points in the paragraph above it is nor the different resource or different representation. Though the data set contains less information the requesting side (filtering this data set) should be considering this and behave accordingly. So again: it is the same resource with the same resource name and the same resource representation.
    0 讨论(0)
  • 2020-11-29 21:47

    The reason why articles on REST resources are abstract is because the concept of a REST resource is abstract. It's basically "whatever thing is accessed by the URL you supply". So, in your example, the resource would be the list of two users starting at offset 5 in some bigger list. Note that, how the resource is implemented is a detail you don't care about unless you are the one writing the implementation.

    Is the following URL a resource?

    The URL is not a resource, it is a label that identifies the resource, it is, if you like, the name of the resource.

    The JSON is a representation of the resource.

    0 讨论(0)
  • 2020-11-29 21:47

    A resource is:

    • a noun
    • that is unique
    • and can be represented as data
    • and has at least one URI

    I go into more detail on my blog post, What, Exactly, Is a RESTful Resource?

    0 讨论(0)
  • 2020-11-29 21:50

    Conceptually you can think about a resource as everything which is accessible on the web using an URL. If you stick to this rule http://api.example.com/users.json?length=2&offset=5 can be considered a resource

    0 讨论(0)
提交回复
热议问题