What is API?
API is an acronym for Application Programming Interface. An API is a tool that allows applications to communicate with each other.
The API operating principle is very simple. An application sends data (known as a request) to a server. The server processes this information and sends back a response. The application then interprets that data and presents it in a readable way.
The response usually comes in JSON format. JSON is an open standard data format that is lightweight and human-readable. It is widely supported in many programming languages.
The big advantage of an API is that developers don't need to know how a server processes this information, which simplifies their work. And also due to this benefit, companies can safely provide their data and functionality to third parties.
APIs are extremely popular. All the most used applications like Facebook, Amazon, and many others apply it.
What is REST?
The API usage growth in all market segments causes the necessity of protocols, standards, and rules that would facilitate to standardize data exchange through the API. The most widespread decision to resolve this issue has become REST.
REST (Representational State Transfer) is a set of web API architecture principles, or constraints, (not a protocol or a standard), that dictate how web service applications communicate over the internet. For this purpose, REST API (also known as a RESTful API) uses the HTTP protocol between client and host server at a predefined set of URLs.
REST API request and response structure
- Method (HTTP verbs: GET, PUT, POST, DELETE, etc.)
- URL (resource and parameters)
- Headers (meta-data of the request)
- Body (the content of the request)
- Status code
- Headers (meta-data of the response)
- Body (the content of the response)
The most common HTTP methods are:
- GET: The get method is used to read (retrieve) data on the server.
- POST: The post method is used to create data.
- PUT: The patch method is used to update data.
- DELETE: The delete method is used to delete data.
- Host name defines the address of the server providing the data
- Base path defines the API product name, which is typically associated with some business entity, as well as its version
- Resource name defines the specific API resource, which further details and clarifies possible API request options
- Query parameters can be used to send supplementary request information, e.g. for filtering, sorting or other actions.
HTTP status codes are three-digit codes delivered in the HTTP header. They indicate whether a specific HTTP request has been successfully completed. They also help to identify the cause of the problem when a resource does not load properly.
These codes are grouped into five classes:
- Informational responses (100–199)
- Successful responses (200–299)
- Redirection messages (300–399)
- Client error responses (400–499)
- Server error responses (500–599)
The most common responses are:
|200||OK||The request has succeeded|
|201||Created||The request has been fulfilled and resulted in a new resource being created|
|400||Bad Request||The request could not be understood by the server due to incorrect syntax|
|401||Unauthorized||The request requires user authentication|
|403||Forbidden||The client does not have access rights to the content|
|404||Not Found||The server has not found anything matching the Request-URI|
|429||Too Many Requests||The user has sent too many requests in a given amount of time (“rate limiting”)|
|500||Internal Server Error||The server encountered an unexpected condition that prevented it from fulfilling the request|
|502||Bad Gateway||The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request|
|503||Service Unavailable||The server is currently unable to handle the request due to a temporary overloading or maintenance of the server|
|504||Gateway Timeout||The server is acting as a gateway and cannot get a response in time for a request|