Dart by Example: HTTP Requests

  1. import 'dart:convert';
  2. import 'package:http/http.dart' as http;
  3. main() async {
  4. // The http package can be used to make HTTP requests using dart:io
  5. // or from the browser via XMLHttpRequests
  6. var query = 'dartlang';
  7. var requestUrl = 'http://hn.algolia.com/api/v1/search?query=$query';
  8. var response = await http.get(requestUrl);
  9. // decode the reponse into a Map
  10. var jsonResponse = json.decode(response.body);
  11. // print a link to the article
  12. var firstResult = jsonResponse['hits'][0];
  13. var firstTitle = firstResult['title'];
  14. var firstLink = firstResult['url'];
  15. print('$firstTitle : $firstLink');
  16. }
  1. $ dart http_request.dart
  2. Dart Lang 1.2 Released : https://www.dartlang.org/

by @jryanio | source | license