Skip to main content

REST API

A server is a computer with special software. A back-end is a program located on a server that can process incoming HTTP requests and has a set of ready-made actions for specific requests.

REST API

API(Application Programming Interface) is a set of well-defined communication rules between various software components. The interface describes what you can ask the program to do and what you will get as a result.

REST (representational state transfer) is a back-end architecture style based on a number of principles that describe how network resources are defined and addressed.

REST API is a back-end built on the REST principle. It serves as a layer between the web application and database. It has a standard interface for accessing resources. It works like a website: you send an HTTP request from the client to the server, and in response, instead of an HTML page, you receive data in JSON format.

Request format

A REST service requires the client to make a request to add, retrieve or modify data. Such request can consist of the following parts.

EntryDescription
HTTP MethodDetermines what operation to perform.
HTTP HeadersEnable the client to transfer additional request-related information.
PathThe path to the resource. All available paths are described in the back-end documentation.
BodyA request block containing data.

HTTP methods

There are several basic HTTP methods for working with a REST service.

MethodDescription
POSTCreating a new resource
GETGetting a set of resources or a single resource
PUTUpdating an existing or creating a new resource
PATCHUpdating an existing resource
DELETEDeleting a resource

HTTP headers

Headers contain additional information related to the content of requests. For example, the type of content that the client can process in the server’s response (the Accept header), or which describes the type of resource that the client sends to the server or the server sends to the client (the Content-Type header).

Accept: text/html
Content-Type: application/json

MIME types are content types. They are used to indicate the content of requests and responses; they consist of a type and a subtype separated by a forward slash /. For example, a text file containing HTML will be described as text/html type. If a file contains CSS, it will be described as text/css. JSON data will be described as application/json. If the client expects text/css, but receives application/json, it will not be able to recognize and process the response content.

Paths

Requests must contain the path to the handled resource. The available paths (endpoints, resources) are described in the back-end documentation.

GET https://bookstore.com/api/orders
Accept: application/json

Such path clearly specifies a resource, even if you have never seen it before, because it is hierarchical and descriptive. Make a request to get a collection of orders.

To get one element of the collection, its identifier is added to the resource. Consider a request to read one order with the identifier 289.

GET https://bookstore.com/api/orders/289
Accept: application/json

The last part of the path is called a dynamic parameter, which is described in the documentation as /resource/:parameter. The resource is permanent, it is the path to the entire collection, whereas the parameter value changes for each of its elements.

Response codes

The server sends a response to the client's request, which contains a status code to inform the client about the operation result. These codes are divided into groups.

GroupDescription
1XXFor information purposes
2XXSuccess codes
3XXDescribe everything related to redirection
4XXIndicate customer errors
5XXIndicate server errors
Note

You do not have to remember all codes from each group – it is enough to know the most common ones. The rest can always be found in the List of HTTP Status Codes.

CodeDescription
200 (OK)Standard response for successful HTTP requests.
201 (Created)Standard response for the HTTP request that resulted in the successful creation of a new resource.
400 (Bad Request)The request cannot be processed due to invalid request syntax or other client error.
401 (Unauthorized)Authorization is required to access the resource.
403 (Forbidden)The client does not have permission to access this resource.
404 (Not Found)The resource is not found. It may have been deleted or may not exist yet.
500 (Internal Server Error)General response to unexpected server crash unless more specific information is available.

Request-Response

Suppose you have an application to view, create, edit and delete customers and orders from a small bookstore, with a back-end hosted at bookstore.com/api. Using your knowledge, describe the request-response process for the back-end with pseudocode.

If you want to get data about all customers, your GET request will look like this.

GET bookstore.com/api/customers
Accept: application/json

To which the server will send you the following response.

Status: 200 OK
Content-Type: application/json
Body: JSON data for all customers

To receive the data for one customer, enter his/her identifier, specifying your GET request.

GET bookstore.com/api/customers/289
Accept: application/json

To which the server will send you the following response.

Status: 200 OK
Content-Type: application/json
Body: JSON customer data

In order to add a new customer, make a POST request.

POST bookstore.com/api/customers
Content-Type: application/json
Body: { "username": "Mango", "email": "mango@gmail.com" }

The server adds a unique identifier and returns the entire object as a result.

Status: 201 Created
Content-type: application/json
Body: { "id": 18674, "username": "Mango", "email": "mango@gmail.com" }