Flutter http methods made Simple

H Awaisha
4 min readFeb 5, 2022

--

Get, Post, Put, Patch and Delete

http request=> rest api integration

________________________________

as mentioned uppove there’s 5 main used http requests methods used in flutter and every other platforms

Let’s start with the most simple one => GET is used to request data from a specified resource.

Second method is Post => POST is used to send data to a server to create a resource.

Third method is Put => PUT is used to send data to a server to update a resource.

Fourth method is Patch => Patch is used to send data to a server to update a resource.

Fifth method is Delete => The DELETE method deletes the specified resource.

_________________________________

Get Vs Post: you can use the two methods to send/receive data from the third party api server but wait what is the difference between them, I’ll explain in a sec.

In Get you can for sure send the data to the server from the client but all your informations will be visible in the request link => url.

In the other hand for Post method your data will be sent to the server as a body not as a parameter and it will not be visible in the request link means more security for your data.

________________________________

Put Vs Patch: you can edit a specific record in your database in each of these two methods but why there’s two methods if they have the same action, I’ll explain in a sec.

In Put you’ll edit a specific record let say by its id, but the main point it will update all the record’s data.

In the other hand for the Patch method you’ll edit your specified record and let’s say by its id, but the main difference than -Put- that you can edit a specific data from your specified data base record.

____________________________________

Enough talking and let us dive in our code +_+

the api used is fake and just for testing => https://reqres.in

type this to your ide terminal: flutter pub add http

second step import the http and convert to your .dart file.

http, convert packages

note the keyword as is used to assign a name for http package to make it easier for us to work with it.

third and most interesting step is the actual coding 😎

I will start with the Get.

http Get

I’ll assume you have a basic understanding for Flutter Futures.

method explanation => first of all we used async + await because we have some action that wants (n) amount of time, it is the response for the request in our case, second we want a url to get the data from, then we will get the response by typing http.get(Url) and as a last step we will return a decoded copy of the response body.

Now Post time.

http Post

method explanation => after defining the url, we will define a dart map of <String, String> key value pairs that will hold our request body, then we will define our response as http.post(Url, body) and the last step is returning a decoded copy of the response.

Put method time.

http Put

method explanation => in put, patch and delete we need the id or any other key for the record, now after we had a url we will type in the end of the url ?id= our specified record id, second step will be define a dart <String, String> key value pairs representing our body or the updates we want to achieve to the record body.

Patch method time.

http Patch

method explanation => it’s the same of put method in the code wise, but there’s one difference between the two methods, mentioned uppove and I hope you did not forget it this fast 😅

nOW Let us delete some db records.

http Delete

method explanation => as the update methods (put, patch) you should specify the record id and type it in the url as ?id= our record id, secondly the response has no need to have a body just http.delete(Url) and usual the last thing is to return the decodedResponseBody

______________________________________________________________

Conclusion: in this article we had a some what deep look at the main five http methods used in third party api integration: Get, Post, Put, Patch and Delete, the difference between Get and Post and the difference between Put and Patch, last thing we talk about is the Delete method and it’s code implementation.

--

--