Do REST API URLs have to look like this?

后端 未结 7 700
孤独总比滥情好
孤独总比滥情好 2020-12-28 18:36

Is it true that to implement a RESTful API, one has to implement a URL structure that looks like this

http://example.com/post/
http://example.com/post/123


        
相关标签:
7条回答
  • 2020-12-28 19:10

    The Idea behind REST is that every resource has it’s own URL and you use the different HTTP methods to interact with those resources. It makes sense to define the URL structure so that the hierarchy between different resources is reflected in the URL, but you don’t have to.

    If you have URLs like this

     /all-posts/
     /first-post
     /some-stuff/second-post
     /third-post
    

    you still could provide an RESTful API to this. The Idea is that a GET to /all-posts/ returns a list of the URLs of every post object and the client uses those URLs to interact with the resources. Basically the URLs should be treated as opaque data by the client.

    As long as the URL that is embedded in the client doesn’t change you also could change the structure without having to change the client.

    Your example URL probably doesn’t belong to a RESTful API, since it contains a method get_title. In REST a URL represents a thing. What is to be done with the thing (should it be modified, should it contents be retrieved, ...) is not part of the URL, for that REST uses the different HTTP methods.

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