Server Response and Data Types
Response components. Status codes. Basic data types
The Response that comes from the server works almost according to the same scheme as Request. It, for obvious reasons, has no request parameters, but the Headers and the Body are included in the response (although they may be empty).
An important difference is the status of the response.
Status code. It comes in the first line of the server response. The status is a three-digit number (the code itself), followed by a phrase explaining it.
It is by the status code that you can find out about the results of the request and understand what actions should be taken next.
All possible status codes are divided into 5 classes. The first digit of the code determines belonging to a particular class. Let's break them down.
1xx — information codes. Report the progress of the request. In real practice, they are rarely used.
2xx — success codes. They report that everything is in order and the request was completed successfully. In response to a GET request, we usually expect to receive a 200 (OK) code. A successful PUT request sends a 201 (Created) code.
3xx — redirects. Indicate that the request should be sent to a different address. An example is code 301 (Moved Permanently), indicating that the required data is now at a new address (the new address itself is passed in the Location header).
4xx — client error codes. The most famous of them - 404 (Not Found), reports that there is no necessary data at the specified address. Other common cases: 400 (Bad Request, syntax error in the request), 401 (Unauthorized, authentication required for access), 403 (Forbidden, access denied).
5xx — server error codes. Report an error on the server side. As an example: 500 (Internal Server Error, any incomprehensible error that cannot be attributed to known code), 503 (Service Unavailable, the server is temporarily unable to process the request for technical reasons)
At this point, we can assume that we have dealt with the basic information for understanding the REST API and the structure of HTTP requests and responses. It remains to clarify only one point - data types. If you have already tried to create your API request in AppMaster, you probably noticed that all data (in parameters, in headers, in body) asks you to specify not only the name, but also the data type.
It is usually pretty obvious to a human how to work with the data, as there is a certain context. Suppose we know that 2 + 2 = 4. We guess that these are numbers and the result of addition will be another number.
But it may not be numbers, but textual data. Then the result of their addition could be the concatenation of strings and 2 + 2 would turn into “22”. Here, so that the computer does not have to think of anything, there is an exact indication of the data type. And at the same time, other tasks are being solved. For example, protection is provided against entering incorrect data; initially, there’s no opportunity to register an e-mail address in the field intended for entering numbers of a phone number.
There are quite a lot of different data types, now we will consider the most basic ones, and in further modules of the course we will get acquainted with the rest.
String — String data type, plain text with no special formatting.
Integer — Integer data type. Can be used for counters or calculations where fractional numbers are not needed
Float — Floating point number. It is used where increased precision is needed and integer values are not enough.
A logical question may arise here. And why not always use Float, why do we need Integer then? But greater accuracy requires more resources. For some small calculations this may be completely invisible, but in the case of large amounts of data, using a reasonable data type can significantly reduce the requirements for computing power and disk space.
Boolean — boolean data type. The simplest data type. It takes one of two values, which are written as True or False. You can often see the designation in the form of 1 (true) and 0 (false).