Book Image

Hands-On RESTful Web Services with TypeScript 3

By : Biharck Muniz Araújo
5 (1)
Book Image

Hands-On RESTful Web Services with TypeScript 3

5 (1)
By: Biharck Muniz Araújo

Overview of this book

In the world of web development, leveraging data is the key to developing comprehensive applications, and RESTful APIs help you to achieve this systematically. This book will guide you in designing and developing web services with the power of TypeScript 3 and Node.js. You'll design REST APIs using best practices for request handling, validation, authentication, and authorization. You'll also understand how to enhance the capabilities of your APIs with ODMs, databases, models and views, as well as asynchronous callbacks. This book will guide you in securing your environment by testing your services and initiating test automation with different testing approaches. Furthermore, you'll get to grips with developing secure, testable, and more efficient code, and be able to scale and deploy TypeScript 3 and Node.js-powered RESTful APIs on cloud platforms such as the Google Cloud Platform. Finally, the book will help you explore microservices and give you an overview of what GraphQL can allow you to do. By the end of this book, you will be able to use RESTful web services to create your APIs for mobile and web apps and other platforms.
Table of Contents (20 chapters)
Free Chapter
1
Section 1: Unraveling API Design
5
Section 2: Developing RESTful Web Services
10
Section 3: Enhancing RESTful Web Services
15
Section 4: Extending the Capabilities of RESTful Web Services

HTTP methods for RESTful services

The use of HTTP verbs allows a clear understanding of what an operation is going to do. In general, the primary or most commonly used HTTP verbs are POST, GET, PUT, PATCH, and DELETE, which stand for create, read, update (PATCH and PUT), and delete, respectively. Of course, there are also a lot of other verbs, but they are not used as frequently:

Method HTTP method description
GET GET is the most common HTTP verb. Its function is to retrieve data from a server at the specified resource.

For example, a request made to the GET https://<HOST>/customers endpoint will retrieve all customers in a list format (if there is no pagination).

There is also the possibility of retrieving a specific customer such as GET https://<HOST>/customers/1234; in this instance, only the customer with the 1234 ID will be retrieved.

It is important to add that the GET request only retrieves data and does not modify any resources; it's considered a safe and idempotent method.

HEAD The HEAD method does exactly what GET does, except that the server replies without the body.
POST

The most common usage of POST methods is to create a new resource.

PATCH The PATCH method is used to apply partial modifications to a resource, such as updating a name or a date, but not the whole resource.
PUT

Different from the PATCH method, the PUT method replaces all of the resource.

DELETE The DELETE method removes a resource.
CONNECT

The CONNECT method converts the connection request to a transparent TCP/IP tunnel, generally to facilitate encrypted communication with SSL (HTTPS) through an unencrypted HTTP proxy.

OPTIONS The OPTIONS method returns the HTTP methods supported by the server for the specified URL.
TRACE The TRACE method returns the same request that is sent to see whether there were changes and/or additions made by intermediate servers.

In order to explain these methods in detail, let's consider a simple entity called Customer. We will imagine that there is an API called Customers available through HTTP and its destination is a NoSQL database like in the following diagram:

Considering that the database is empty, what should happen if we call the GET method by pointing to GET /CUSTOMERS? If you think that it should retrieve nothing, then you are absolutely right! Here is a request example:

GET https://<HOST>/customers

And here is the response:

[]

So, considering that there is no customer yet, let's create one using the POST method:

Request endpoint Sample Request Status code response
POST /customers
{
"first_name":"John",
"last_name":"Doe"
}
HTTP status code 201

Considering the POST method is successful, the HTTP status code should be 201 (we will talk more about status codes in Chapter 2, Principles of Designing RESTful APIs) and calling the GET method now; the response should be similar to the following code block (we're considering that there is just one single consumer of this endpoint and we are that consumer, so we can guarantee the data behavior into the server):

Request endpoint Sample Request Status code response
GET /customers
[
{
"id" : 1,
"first_name":"John",
"last_name":"Doe"
}
]
The HTTP status code is 200.

What if we add two more customers and call the GET method again? Take a look at the following code block:

Request endpoint Sample Response Status code response
GET /customers
[
{
"id" : 1,
"first_name" : "John",
"last_name" : "Doe"
},
{
"id" : 2,
"first_name" : "Mary",
"last_name" : "Jane"
},
{
"id" : 3,
"first_name" : "Jane",
"last_name" : "Doe"
}
]
The HTTP status code is 200.

Nice, so if we call the GET method for a specific ID, the response should be like the following:

Request endpoint Sample Response Status code response
GET /customers/1
{
"id" : 1,
"first_name":"John",
"last_name":"Doe"
}
The HTTP status code is 200.

We can also call POST methods for parent resources, such as /customers/1/orders, which will create new resources under the customer with ID 1. The same applies to the GET method, but only to retrieve the data as mentioned previously.

Remember that the GET method should never modify any resource.

Okay, now that we know how to create and retrieve the resources, what if we want to change any information, such as the last name for John Doe? This one is easy—we just have to call the PATCH method in the same way that we called the POST method:

Request endpoint Sample Request Status code response
PATCH /customers/1
{
"last_name" : "Doe Jr."
}
The HTTP status code is 204.

We can also change the information in a parent using a path as follows:

Request endpoint Sample Request Status code response
PATCH /customers/1/orders
{
"status" : "closed"
}
The HTTP status code 204.

And what if we need to change the whole resource? In that case, we should use the PUT method:

Request endpoint Sample Request Status code response
PUT /customers/1
{
"first_name" : "Wolfgang",
"last_name" : "Amadeus Mozart"
}
The HTTP status code is 201.

Instead of PATCH, you can use the PUT method. To do so, all the parameters are required to be passed as the body into the request, even those parameters that haven't changed.

Finally, if you want to remove a resource, use the DELETE method:

Request endpoint Status code response
PUT /customers/1
The HTTP status code is 204.