What exactly is RESTful programming?

前端 未结 30 3213
Happy的楠姐
Happy的楠姐 2020-11-21 06:02

What exactly is RESTful programming?

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

    Talking is more than simply exchanging information. A Protocol is actually designed so that no talking has to occur. Each party knows what their particular job is because it is specified in the protocol. Protocols allow for pure information exchange at the expense of having any changes in the possible actions. Talking, on the other hand, allows for one party to ask what further actions can be taken from the other party. They can even ask the same question twice and get two different answers, since the State of the other party may have changed in the interim. Talking is RESTful architecture. Fielding's thesis specifies the architecture that one would have to follow if one wanted to allow machines to talk to one another rather than simply communicate.

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

    REST defines 6 architectural constraints which make any web service – a true RESTful API.

    1. Uniform interface
    2. Client–server
    3. Stateless
    4. Cacheable
    5. Layered system
    6. Code on demand (optional)

    https://restfulapi.net/rest-architectural-constraints/

    0 讨论(0)
  • 2020-11-21 06:46

    An architectural style called REST (Representational State Transfer) advocates that web applications should use HTTP as it was originally envisioned. Lookups should use GET requests. PUT, POST, and DELETE requests should be used for mutation, creation, and deletion respectively.

    REST proponents tend to favor URLs, such as

    http://myserver.com/catalog/item/1729
    

    but the REST architecture does not require these "pretty URLs". A GET request with a parameter

    http://myserver.com/catalog?item=1729
    

    is every bit as RESTful.

    Keep in mind that GET requests should never be used for updating information. For example, a GET request for adding an item to a cart

    http://myserver.com/addToCart?cart=314159&item=1729
    

    would not be appropriate. GET requests should be idempotent. That is, issuing a request twice should be no different from issuing it once. That's what makes the requests cacheable. An "add to cart" request is not idempotent—issuing it twice adds two copies of the item to the cart. A POST request is clearly appropriate in this context. Thus, even a RESTful web application needs its share of POST requests.

    This is taken from the excellent book Core JavaServer faces book by David M. Geary.

    0 讨论(0)
  • 2020-11-21 06:46

    This is very less mentioned everywhere but the Richardson's Maturity Model is one of the best methods to actually judge how Restful is one's API. More about it here:

    Richardson's Maturity Model

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

    This is what it might look like.

    Create a user with three properties:

    POST /user
    fname=John&lname=Doe&age=25
    

    The server responds:

    200 OK
    Location: /user/123
    

    In the future, you can then retrieve the user information:

    GET /user/123
    

    The server responds:

    200 OK
    <fname>John</fname><lname>Doe</lname><age>25</age>
    

    To modify the record (lname and age will remain unchanged):

    PATCH /user/123
    fname=Johnny
    

    To update the record (and consequently lname and age will be NULL):

    PUT /user/123
    fname=Johnny
    
    0 讨论(0)
  • 2020-11-21 06:47

    If I had to reduce the original dissertation on REST to just 3 short sentences, I think the following captures its essence:

    1. Resources are requested via URLs.
    2. Protocols are limited to what you can communicate by using URLs.
    3. Metadata is passed as name-value pairs (post data and query string parameters).

    After that, it's easy to fall into debates about adaptations, coding conventions, and best practices.

    Interestingly, there is no mention of HTTP POST, GET, DELETE, or PUT operations in the dissertation. That must be someone's later interpretation of a "best practice" for a "uniform interface".

    When it comes to web services, it seems that we need some way of distinguishing WSDL and SOAP based architectures which add considerable overhead and arguably much unnecessary complexity to the interface. They also require additional frameworks and developer tools in order to implement. I'm not sure if REST is the best term to distinguish between common-sense interfaces and overly engineered interfaces such as WSDL and SOAP. But we need something.

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