What is the REST API?

To answer this question, we will look at its full form, first of all. REST is short for REpresentational State Transfer, and API stands for Application Program Interface. To put it in simpler language, REST is an application program interface that is used for requesting HTTP requests to get or be able to send data from one computer to another.

Flutter gives us access to http Packages with the view of consumer HTTP resources. http Packages is a useful and important future-based library responsible for features such as await, async, and various other methods. It also gives us a class and http so as to perform web requests.

Since we have covered what REST API means, let us now have a look at the core methods that http packages have to offer us.

  • get Method: This method requests the URL and calls for the data in JSON (JavaScript Object Notation) format.
  • post Method: This method uses the URL through the POST method to submit the data, and it returns a Future<response>. This method can be used to send data such as information of the user, file uploads and others on the web.
  • delete Method: This method delete is used for deleting the data in a specified resource.
  • Head Method: This method is used for requests in the form of the URL. It returns the response as a Future<response>. This method is very similar to the get Method; the only difference is that get is without the response body.
  • read Method: This method is also used for request in the form of the URL in order to get the response as a Future<string>. In simpler words, it is used for reading the data.
  • put Method: This method is used for updating capabilities. It uses the URL with the intention of updating the data, which was specified by the user by using the put method.
  • patch Method: This method is not very common; however, it can be proven useful at certain times. It is similar to the put Method as it is also used for updating the data. 

Let us now move ahead in this topic and explore a lot more about the meanings of the different terminologies used above in this article.

Setting up the project and deployment of REST applications.

STEP 1: Installing and adding http dependency.

STEP 2: Importing the http package as http.

STEP 3: Also, import the given package for the purpose of converting JSON data.

STEP 4: Creating a request in our code.

STEP 5: Showing the error message in case there is an error.

If the response code is 200, then the requested data will be displayed; otherwise, it will throw an error.

STEP 6: Now, in this step, we will be decoding the JSON data.

STEP 7: We will be creating a class by the name “Employee.”

This class, “Employee,” will contain the details of the employees working in an organization. Those details are having an employee’s: id (employee ID), fname (first name), lname (last name) and age.

STEP 8: Next step is to create an EmployeeList class.

STEP 9: We will also create EmployeeNumber class for calculating the number of employees working in that same organization.

STEP 10: The last step is to display the response on the screen by writing the following code:

The JSON file will look like this:

Summary

In this article, we were introduced to REST API (which is short for REpresentational State Transfer), which, if we put it in layman’s language, is responsible for fetching data from the internet and putting it in our flutter application so that the users/clients can enjoy some of the additional features implemented by the developer. Next, we learned about the http package and which methods are there in it. We saw how to get, post, delete, head, read, put, and patch methods and their significance. In the last part of this article, we saw and comprehended the process of setting up the project and deployment of REST API.

Pin It on Pinterest

Share This