Do REST API URLs have to look like this?

后端 未结 7 699
孤独总比滥情好
孤独总比滥情好 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 18:47

    The structure of your URLs doesn't matter. What does matter is that each URL identifies exactly 1 resource. Each resource can have multiple URLs that point to it but each URL should only point to 1 resource.

    0 讨论(0)
  • 2020-12-28 18:54

    Example URLs:

    GET http://del.icio.us/api/
    GET http://del.icio.us/api/peej/tags/
    GET http://del.icio.us/api/peej/tags/test
    DELETE http://del.icio.us/api/peej/bookmarks/[hash]
    
    0 讨论(0)
  • 2020-12-28 19:03

    RESTful URI design is all about resources access and they should be structured in the RESTful manner, so you should not have any query strings.

    e.g. of GET

    authors/

    authors/1

    authors/1/books

    authors/1/books/10

    authors/1/books/10/summary

    etc.

    Anything and everything is called RESTfull these days, just look at some of the responses by it's inventor Dr Roy Fielding and you'll get some ideas. It is worth doing some reading on the subject.

    P.S you do not need post,get etc in your URIs, HTTP protocol is at present mostly used for consuming REST APIs and you can pass verb as a part of the call. Also there is a concept of content negotiation i.e you can request any available format from REST API (json,xml atc).

    0 讨论(0)
  • 2020-12-28 19:05

    The REST concept is really based on the fact that it is URL driven, and not driven by large data-blobs. With REST, you don't have to pass a giant soap request to invoke a method - your method call/object creation/whatever you want to do is invoked simply by the URL, and the verb you used vs that URL.

    0 讨论(0)
  • 2020-12-28 19:07

    A key aspect of REST is that the url is the resource. a uri like

    http://example.com/script.php?etc-etc-etc
    

    doesn't put the resource identifier in the resource portion of the uri. that's not to say that a RESTful API shouldn't ever use get parameters; in fact, that's just fine:

    http://example.com/posts?sort=date_asc&offset=20&limit=10
    

    might be a great way to get the URI's of the 3rd page of oldest posts. However, using get parameters in this way should only be used in requests where the method is also GET. PUT and especially POST methods should really use simple uri's with the resource that will be affected in only the path portion.

    0 讨论(0)
  • 2020-12-28 19:08

    You don't have to design your URI structure like that. It could also be /some_obscure_string/base64_encoded_title/unique_id. This could also be RESTful, depending on several other factors.

    But there are several best practices on how to design URIs in a RESTful web application and being as simple and as human readable as possible is one of them.

    Your example http://example.com/script.php?method=get_title&blogid=123 could also be RESTful, but the query parameters indicate that some kind of RPC- or RMI-over-HTTP is used instead.

    To sum it up: Don't put too much thought into your URI design. This will come automatically with a good and proper RESTful design of your application.

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