Make Http Request in Dart

Introduction

HTTP request means sending a request to a server and getting a response from the server. In this tutorial, we will learn how to make http request in dart with examples.

Create Dart Project

Before starting this tutorial, you need to create a dart project. Make sure you have installed the dart sdk in your system. First, open the command prompt/terminal and type the following command to create a dart project.

dart create <project_name>

This will create a simple dart project with some ready-made code.

Steps To Create Dart Project

  • Open folder location on command prompt/terminal.
  • Type dart create my_app
  • Type cd my_app
  • Type code . to open project with visual studio code
  • To check main dart file go to bin/my_app.dart and edit your code.

Run Dart Project

First, open the project location on the command/terminal and run the project with this command.

dart run

Download Http Package

To make HTTP request in dart, we need to download the http package. To download the http package, open the pubspec.yaml file and add the following line in the dependencies section. You can get http package from here.

dependencies:
  http: ^0.13.5

Make HTTP Get Request

When you want to get data from the server, you need to make a get request. To make HTTP get request in dart, you can use get() method on the http client instance.

// import http package
import 'package:http/http.dart' as http;


void main() async {
  var url = Uri.parse('https://jsonplaceholder.typicode.com/posts/1');
  // make http get request
  var response = await http.get(url);
  // check the status code for the result  
  if (response.statusCode == 200) {
    print(response.body);
  } else {
    print('Request failed with status: ${response.statusCode}.');
  }  

}
Run Online
Info

Note: If the status code is 200, it means the request is successful and you can get the response body otherwise the request is failed and you can get error message.

Make HTTP Post Request

When you want to send data to the server, you need to make a post request. To make HTTP post request in dart, you can use post() method on the http client instance.

// import http package
import 'package:http/http.dart' as http;

void main() async {
  var url = Uri.parse('https://jsonplaceholder.typicode.com/posts');
  // make http post request
  var response = await http.post(url, body: {'title': 'foo', 'body': 'bar', 'userId': '1'});
  // check the status code for the result  
  if (response.statusCode == 201) {
    print(response.body);
  } else {
    print('Request failed with status: ${response.statusCode}.');
  }  

}
Run Online

Make HTTP Put Request

When you want to update data on the server, you need to make a put request. To make HTTP put request in dart, you can use put() method on the http client instance.

// import http package
import 'package:http/http.dart' as http;

void main() async {
  var url = Uri.parse('https://jsonplaceholder.typicode.com/posts/1');
  // make http put request
  var response = await http.put(url, body: {'title': 'foo', 'body': 'bar', 'userId': '1'});
  // check the status code for the result  
  if (response.statusCode == 200) {
    print(response.body);
  } else {
    print('Request failed with status: ${response.statusCode}.');
  }  

}
Run Online

Make HTTP Patch Request

When you want to update a part of data on the server, you need to make a patch request. To make HTTP patch request in dart, you can use patch() method on the http client instance.

// import http package
import 'package:http/http.dart' as http;

void main() async {
  var url = Uri.parse('https://jsonplaceholder.typicode.com/posts/1');
  // make http patch request
  var response = await http.patch(url, body: {'title': 'foo'});
  // check the status code for the result  
  if (response.statusCode == 200) {
    print(response.body);
  } else {
    print('Request failed with status: ${response.statusCode}.');
  }  

}
Run Online

Make HTTP Delete Request

When you want to delete data from the server, you need to make a delete request. To make HTTP delete request in dart, you can use delete() method on the http client instance.

// import http package
import 'package:http/http.dart' as http;

void main() async {
  var url = Uri.parse('https://jsonplaceholder.typicode.com/posts/1');
  // make http delete request
  var response = await http.delete(url);
  // check the status code for the result  
  if (response.statusCode == 200) {
    print(response.body);
  } else {
    print('Request failed with status: ${response.statusCode}.');
  }  

}
Run Online
Info

Note: If the status code is 200, it means the request is successful and you can get the response body otherwise the request is failed and you can get error message.