...

/

HTTP Requests

HTTP Requests

Learn to make HTTP calls over REST and GraphQL protocols through GetConnect.

Introduction to GetConnect

GetConnect is GetX’s built-in networking library. It allows us to make all kinds of HTTP requests, whether REST or GraphQL. It also provides a handful of configuration options and advanced features we’ll cover in the upcoming lesson. But for now, let’s explore the HTTP methods!

REST API

Representational State Transfer (REST) is a standard API architecture for the web. It helps us integrate server-side programs with client-facing applications in the form of microservices. These microservices are functions written on the server side that can be executed by making HTTP requests from the client.

In Flutter, we can make HTTP requests by using any one of the available networking libraries, such as http and dio. But since we are in the GetX ecosystem, we can use GetX’s internal networking library called GetConnect.

To make HTTP requests, we must first create an instance of the GetConnect class.

Press + to interact
final GetConnect connect = GetConnect();

Great, now we can use this instance to make requests by calling the relevant HTTP methods.

HTTP methods

There are essentially five kinds of HTTP methods under the REST architecture:

  1. GET

  2. POST

  3. PUT

  4. DELETE

  5. PATCH

Let’s go through each of them:

The GET method

GET method is used retrieve the data from the server. The method simply takes in the URL of the location where the data resides and fetches it to the client. Here's how we write the GET method with GetConnect:

Press + to interact
// GET method
final Response response =
await connect.get('https://jsonplaceholder.typicode.com/todos/1');

We use the connect instance to call the get method. We provide it a URL that would fetch a TODO from the server.

The POST method

POST method is used to add a new entry to the database. We provide it with the location of the directory in the form of URL, and a map of the data we wish to upload. Here's how it looks:

Press + to interact
// POST method
final Response response = await connect.post(
'https://jsonplaceholder.typicode.com/posts',
{
'title': 'foo',
'body': 'bar',
'userId': 1,
},
);

Here, we are creating a new post in the server by providing its title, body and user ID.

The PUT method

PUT method is used to update existing data on the server. It takes in the URL pointing to the exact resource that needs to update and the new data that will replace it. The PUT method looks like this:

Press + to interact
// PUT method
final Response response = await connect.put(
'https://jsonplaceholder.typicode.com/posts/1',
{
'id': 1,
'title': 'foo',
'body': 'bar',
'userId': 1,
'name': 'Aachman',
},
);

In the code snippet above, we update the post by providing the URL of the post and the data of the new post. The existing post is entirely replaced with the new one.

The DELETE method

DELETE method is used to delete a data entry from the server database. Simply provide it the URL to the data entry and it deletes it. Here’s what the method looks like:

Press + to interact
// DELETE method
final Response response =
await connect.delete('https://jsonplaceholder.typicode.com/posts/1');

We provide the above method with the URL to the post and it gets deleted from the server.

The PATCH method

PATCH method is also used to update the data on the server, but unlike the PUT method, it does not entirely replace the data entry. Instead, it allows us to partially modify its data. We can be selective with the fields that we wish to update and provide that to ...