Part 1: What is REST In this part we will take a closer look at how communication occurs between the client and the server. Along the way, we will reveal new terms and give explanations for them. To make everything (become) clear, we will analyze client-server communication using the example of some RESTful application. Let's say we are developing a web application that is capable of storing information about customers and their orders. Those. our system is capable of manipulating some entities: creating them, editing them, deleting them, and providing information about them. These entities will be:
- clients - clients;
- orders - customer orders;
- items - goods.
Requests
Client requests are almost always made over HTTP. In general, HTTP requests consist of several components:- HTTP method;
- title;
- URI;
- request body.
URI and Resources
Data that clients obtain or modify through requests is called resources. The basis of client-server interaction is the manipulation of resources. Resources in REST are anything that can be given a name. In a sense, these are like classes in Java. In Java we can create a class for anything. It’s the same in REST - a resource can be anything: a user, a document, a report, an order. All this can be either an abstraction of some entity or something concrete, for example, a file - a picture, video, animation, PDF file. For our example, we have 3 resources:- clients - clients;
- orders - customer orders;
- items - goods.
- /clients - URI of all existing clients;
- /clients/23 - URI of a specific client, namely the client with ID=23;
- /clients/4 - URI of a specific client, namely the client with ID=4.
- /clients/4/orders — URI of all orders of client No. 4;
- /clients/1/orders/12 - URI of order No. 12 of client No. 1.
- /clients/1/orders/12/items — URI of the list of all products in order No. 12 made by client No. 1.
HTTP method
HTTP Method (English HTTP Method) is a sequence of any characters, except for controls and separators, which indicates the main operation on a resource. There are several common HTTP methods. We list those that are most often used in RESTful services:- GET - used to obtain information about a specific resource (via ID) or a collection of resources;
- POST - used to create a new resource;
- PUT - used to change a resource (via ID);
- DELETE - used to delete a resource (via ID).
Headings
Requests, as well as responses, contain HTTP headers. They send additional information about the request (or response). Headers are key-value pairs. You can read the list of the most common headings on the Wikipedia page . With REST, clients can often send an Accept header in their request to the server. It is needed to let the server know in what format the client expects to receive a response from it. Various format options are presented in the so-called MIME type list. MIME (Multipurpose Internet Mail Extensions) is a specification for encoding information and formatting messages so that they can be sent over the Internet. Each MIME type consists of two parts, separated by a slash: a type and a subtype. Examples of MIME types for different types of files:- text - text/plain, text/css, text/html;
- image - image/png, image/jpeg, image/gif;
- audio - audio/wav, audio/mpeg;
- video - video/mp4, video/ogg;
- application - application/json, application/pdf, application/xml, application/octet-stream.
Accept:application/json
This header tells the server that the client expects to receive a response in JSON format.
Request body
A message sent by the client to the server. Whether a request has a body or not depends on the type of HTTP request. For example, GET and DELETE requests typically do not contain any request body. But PUT and POST can contain: it’s all about the functional purpose of the request type. After all, to receive data and delete it by id (which is transmitted in the URL), you do not need to send additional data to the server. But to create a new resource (POST request), you need to transfer this resource. The same goes for modifying an existing resource. In REST, XML or JSON formats are most often used to transmit the request body. The most common format is JSON. Suppose we want to send a request to the server, and in it create a new resource. If you remember, as an example we looked at an application that manages customer orders. Let's say we want to create a new client. In our case, we store the following information about clients: Name Email Phone number Then the body of such a request could be the following JSON:{
"name" : "Amigo",
"email" : "amigo@jr.com",
"phone" : "+7 (191) 746-43-23"
}
Putting requests together
So, we have looked at what a client request can consist of. Let us now give some examples of queries with descriptionsRequest | Description |
---|---|
|
Get information about client No. 23 in json or xml format |
|
Create a new client with the following fields: Name - Amigo Email - amigo@jr.com Tel. — +7 (191) 746-43-23 |
|
Edit client #1 as follows: Name - Ben Email - bigben@jr.com Tel. — +380 (190) 346-42-13 |
|
Delete order No. 6 from customer No. 12 from the system |
Answers
Let's say a few words about the server's responses. The answer usually consists of the following parts:- response code;
- headers;
- response body.
HTTP response codes
Let's take a closer look at HTTP response codes. Here is a quote from Wikipedia: HTTP status code is part of the first line of the server response for requests via the HTTP protocol. It is an integer with three decimal digits. The first digit indicates the class of the condition. The response code is usually followed by an explanatory phrase in English separated by a space, which explains to the person the reason for this particular response. Examples:- 201 Created;
- 401 Unauthorized;
- 507 Insufficient Storage.
- 1ХХ - informational;
- 2ХХ - inform about cases of successful acceptance and processing of a client’s request;
- 3XX - inform the client that in order to successfully complete the operation, it is necessary to make another request, usually using a different URI;
- 4ХХ - client error. For example, an incorrectly constructed request or the well-known 404 Not Found code, which can occur when a client requests a non-existent resource;
- 5ХХ - server error. Returned to the client if the operation fails due to the fault of the server.
GO TO FULL VERSION