Blog   ❯   Author: Fabio MoschiniDate: 22.04.2023

Introduction to REST services

alt text for image

What is REST

REST, which stands for REpresentational State Transfer, is an established architecture for creating Web APIs. REST services can use the HTTP protocol (as well as other protocols like SMTP, SNMP) for data exchange. The REST architecture guidelines indicate using a specific HTTP method for a particular type of call.
The set of HTTP methods (also called verbs), such as GET, POST, PUT, and DELETE, simplifies the correspondence between CRUD actions (Create, Read, Update, Delete) and the HTTP calls we will make:

HTTP MethodCRUD OperationDescription
POSTCreateCreates a new resource
GETReadGets an existing resource
PUTUpdateUpdates (replaces) a resource or modifies its state
DELETEDeleteDeletes a resource
PATCHUpdate (partial)Partially updates the resource
OPTIONSReturns information about a specific resource (allowed methods for a resource)

REST service calls are made by specifying the HTTP method, URL, resource, and any information related to the type of request. For example, to invoke the data reading service, we could make a GET request to the address:

https://www.servername.cloud/api/articles/3

In this example, the URL is https://www.servername.cloud, while api is the directory that exposes REST services within the server. The resource we want to access for reading is articles (these could be articles available in a warehouse), and finally 3 represents the key, or identifier, of the article we want to read.
If the call is successful, the service will respond by sending the resource data to the client. The response format can be JSON, XML, HTML, or text.

Response Codes

After invoking a REST service, to understand if our request was successful or not, we must verify the status sent by the server. It's a 3-digit numeric code, where the first digit defines its type:
  • 1xx: information codes
    Example:
    • the code 100 Continue notifies the client that the initial part of the request has been received and hasn't been rejected by the server; the client should send the remaining part of the request or ignore the code if it has already sent the rest.
  • 2xx: success codes
    Example:
    • the code 200 OK indicates that the request was successful;
    • the code 201 Created indicates that the request was successful and the resource was created (this code is returned, for example, with a POST request);
    • the code 202 Accepted indicates that the request has been received but not yet completed. It is typically used in batch processing requests.
  • 3xx: redirection codes
    Example:
    • the code 301 Moved Permanently indicates that the requested resource's URL has been permanently changed. The new URL is indicated in the Location field of the header.
  • 4xx: client error codes
    Example:
    • the code 400 Bad Request indicates that the request cannot be processed by the server due to incorrect syntax. The client should not repeat the request without modifying it first;
    • the code 401 Unauthorized indicates that the request requires user authentication. The client can repeat the same request after properly setting the Authorization field;
    • the code 404 Not Found indicates that the server cannot find the requested resource, for example, via GET;
    • the code 409 Conflict indicates that the server cannot complete the request due to a conflict with the current state of the resource.
  • 5xx: server error codes
    Example:
    • the code 500 Internal Server Error indicates that the server encountered an unexpected condition that prevented it from fulfilling the request;
    • the code 501 Not Implemented indicates that the HTTP method is not supported by the server and therefore cannot be handled;
    • the code 503 Service Unavailable indicates that the server is not ready to handle the request.

Examples with Postman

Let's look at some examples of REST services that can be invoked without any authentication method.
We will use the open-source service https://randomuser.me, through which you can generate random user data.
To test this service, we will use Postman, a tool that allows, among other things, to invoke REST APIs. Postman can be downloaded from this address.
After installing and launching it, simply insert a new request with the GET method and enter the URL https://randomuser.me/api/:

alt text for image


To execute the request to the REST service, simply click on Send. In the lower panel, you can verify the success of the request by checking that the status is 200 OK. In this case, the Body panel displays the request output, which is the random data of a user:
{ "results": [ { "gender": "male", "name": { "title": "Mr", "first": "Alberto", "last": "Rodríguez" }, "location": { "street": { "number": 3194, "name": "Calle de Toledo" }, "city": "Pamplona", "state": "Ceuta", "country": "Spain", "postcode": 23083, "coordinates": { "latitude": "-30.5412", "longitude": "-177.6515" }, "timezone": { "offset": "-5:00", "description": "Eastern Time (US & Canada), Bogota, Lima" } }, "email": "alberto.rodriguez@example.com", "login": { "uuid": "0c35aa62-8afd-4d1d-b1b4-df22d069ea95", "username": "sadfrog876", "password": "admin", "salt": "GN811hZC", "md5": "f532b619eb627e5f1a58d642d7ce99bd", "sha1": "f9b6003152bfaab013c513edc0f308b860395afe", "sha256": "b0e25177d2cab23481bac1f1c40f791bbaf41508e6049ad34d8b7a594a6ef19c" }, "dob": { "date": "1999-10-08T20:25:03.278Z", "age": 23 }, "registered": { "date": "2005-03-17T17:01:10.647Z", "age": 17 }, "phone": "980-469-663", "cell": "612-768-683", "id": { "name": "DNI", "value": "68093680-B" }, "picture": { "large": "https://randomuser.me/api/portraits/men/18.jpg", "medium": "https://randomuser.me/api/portraits/med/men/18.jpg", "thumbnail": "https://randomuser.me/api/portraits/thumb/men/18.jpg" }, "nat": "ES" } ], "info": { "seed": "d5d601bdff24dc24", "results": 1, "page": 1, "version": "1.4" } }
It's possible to generate data for multiple users with a single service call.
To do this, simply modify the URL to: https://randomuser.me/api/?results=3 to generate data for three users.
You can also decide which information to generate for each user. For example, to have only the data related to name, email address, and phone number, you can use this API: https://randomuser.me/api/?inc=name,email,phone.
The result is:
{ "results": [ { "name": { "title": "Mr", "first": "Josif", "last": "Stojaković" }, "email": "josif.stojakovic@example.com", "phone": "016-8403-080" } ], "info": { "seed": "883a251f1eccb431", "results": 1, "page": 1, "version": "1.4" } }
Of course, you can combine multiple parameters in the service call by separating them with the & character:
https://randomuser.me/api/?results=3&inc=name,email,phone
The service response will be:
{ "results": [ { "name": { "title": "Ms", "first": "Clara", "last": "Guerrero" }, "email": "clara.guerrero@example.com", "phone": "(665) 211 8714" }, { "name": { "title": "Ms", "first": "Eusébia", "last": "Pinto" }, "email": "eusebia.pinto@example.com", "phone": "(53) 5129-4386" }, { "name": { "title": "Mrs", "first": "Radmila", "last": "Gayduk" }, "email": "radmila.gayduk@example.com", "phone": "(097) Q62-4573" } ], "info": { "seed": "734e7ed69948dbdd", "results": 3, "page": 1, "version": "1.4" } }

In the next article, we will see how to create a set of APIs to handle CRUD operations on a resource.