Use the http package to simplify access to HTTP resources using the command line. Although the dart:io core library has primitives for HTTP clients (see HttpClient ), the http package makes GET, POST, etc. easier.
First add http to your pubspec dependencies:
name: sample_app description: My sample app. dependencies: http: any
Install the package. Run this on the command line or using the Dart editor:
pub install
Import the package:
// inside your app import 'package:http/http.dart' as http;
Make a GET request. The get() function returns a Future .
http.get('http://example.com/hugs').then((response) => print(response.body));
It is best to return the future from a function using get() :
Future getAndParse(String uri) { return http.get('http://example.com/hugs') .then((response) => JSON.parse(response.body)); }
Unfortunately, I could not find any official documents. So I had to view the code (which has good comments): https://code.google.com/p/dart/source/browse/trunk/dart/pkg/http/lib/http.dart
source share