Send GET Request using Dart

http package

  1. Add http package to pubspec.yaml file:
dependencies:
  http: ^0.13.1
  1. Install package from the command line:
pub get
  1. Send GET request:

We can use get() function to send GET request to the given URL. Function returns a Future that completes to Response object.

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

void main() async {
  var url = Uri.parse('https://httpbin.org/get');
  var response = await http.get(url);
  String content = response.body;

  print(content);
}

Or we can use read() function which returns a Future that completes to response body.

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

void main() async {
  var url = Uri.parse('https://httpbin.org/get');
  String content = await http.read(url);

  print(content);
}

Leave a Comment

Cancel reply

Your email address will not be published.