What is the difference between a REST API and a normal API (which prints a JSON response)?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Design RESTful service with multiple ids
There is no difference at all. REST describes a way of interacting with a HTTP server, not what the server should return in response. Most web apps interact with the server side by POST or GET requests with any additional information needed to fulfil the request in a form submission for POST or the query string for GET. So if you want to delete something from the server they typically do POST with a form that contains data that specifies a resource along with an instruction to delete it.
However, HTTP implements methods (also known as verbs) other than GET or POST. It also implements, amongst others, HEAD (return the same headers you would have done for a GET, but with no response body), PUT (Take the request body and store its content at whatever URL the PUT request was made to), and DELETE (Delete whatever resource exists at the specified URL). A REST interface simply makes use of these additional verbs to convay the meaning of the request to the server.
Browsers typically only support GET and POST for "normal" (non-XHR) requests, but tools like Curl can issue the full set of HTTP verbs. You can also use additional verbs with XHR-based techniques such as AJAX.
You will still have to provide a traditional non-REST API for browsers to use, unless you're making javascript and XHR support a requirement for using your app.
REST mostly just refers to using the HTTP protocol the way it was intended. Use the
GET
HTTP method on a URL to retrieve information, possibly in different formats based on HTTPAccept
headers. Use thePOST
HTTP method to create new items on the server,PUT
to edit existing items,DELETE
to delete them. Make the API idempotent, i.e. repeating the same query with the same information should yield the same result. Structure your URLs in a hierarchical manner etc.REST just is a guiding principle how to use URLs and the HTTP protocol to structure an API. It says nothing about return formats, which may just as well be JSON.
That is opposed to, for example, APIs that send binary or XML messages to a designated port, not using differences in HTTP methods or URLs at all.