What is the difference between REST and HTTP protocols?

前端 未结 5 768
猫巷女王i
猫巷女王i 2021-01-30 04:27

What is the REST protocol and what does it differ from HTTP protocol ?

5条回答
  •  太阳男子
    2021-01-30 05:06

    All the answers are good.

    I hereby add a detailed description of REST and how it uses HTTP.

    REST = Representational State Transfer

    REST is a set of rules, that when followed, enable you to build a distributed application that has a specific set of desirable constraints.

    It is stateless, which means that ideally no connection should be maintained between the client and server.

    It is the responsibility of the client to pass its context to the server and then the server can store this context to process the client's further request. For example, session maintained by server is identified by session identifier passed by the client.

    Advantages of Statelessness:

    1. Web Services can treat each method calls separately.
    2. Web Services need not maintain the client's previous interaction.
    3. This in turn simplifies application design.
    4. HTTP is itself a stateless protocol unlike TCP and thus RESTful Web Services work seamlessly with the HTTP protocols.

    Disadvantages of Statelessness:

    1. One extra layer in the form of heading needs to be added to every request to preserve the client's state.
    2. For security we may need to add a header info to every request.

    HTTP Methods supported by REST:

    GET: /string/someotherstring:
    It is idempotent(means multiple calls should return the same results every time) and should ideally return the same results every time a call is made

    PUT:
    Same like GET. Idempotent and is used to update resources.

    POST: should contain a url and body
    Used for creating resources. Multiple calls should ideally return different results and should create multiple products.

    DELETE:
    Used to delete resources on the server.

    HEAD:

    The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The meta information contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request.

    OPTIONS:

    This method allows the client to determine the options and/or requirements associated with a resource, or the capabilities of a server, without implying a resource action or initiating a resource retrieval.

    HTTP Responses

    Go here for all the responses.

    Here are a few important ones:
    200 - OK
    3XX - Additional information needed from the client and url redirection
    400 - Bad request
    401 - Unauthorized to access
    403 - Forbidden
    The request was valid, but the server is refusing action. The user might not have the necessary permissions for a resource, or may need an account of some sort.

    404 - Not Found
    The requested resource could not be found but may be available in the future. Subsequent requests by the client are permissible.

    405 - Method Not Allowed A request method is not supported for the requested resource; for example, a GET request on a form that requires data to be presented via POST, or a PUT request on a read-only resource.

    404 - Request not found
    500 - Internal Server Failure
    502 - Bad Gateway Error

提交回复
热议问题