Skip to content

Instantly share code, notes, and snippets.

@flutterdevrelgists
Created November 12, 2021 21:53
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save flutterdevrelgists/4a68e553746602d851ab3da6aeafc3dd to your computer and use it in GitHub Desktop.
Save flutterdevrelgists/4a68e553746602d851ab3da6aeafc3dd to your computer and use it in GitHub Desktop.
HTTP example
import 'dart:convert' as convert;
import 'package:http/http.dart' as http;
void main(List<String> arguments) async {
// This example uses the Google Books API to search
// for books about HTTP. For details, see
// https://developers.google.com/books/docs/overview
final url = Uri.https(
'www.googleapis.com',
'/books/v1/volumes',
{'q': '{http}'},
);
// Await the HTTP GET response, then decode the
// JSON data it contains.
final response = await http.get(url);
if (response.statusCode == 200) {
final jsonResponse = convert.jsonDecode(response.body);
final itemCount = jsonResponse['totalItems'];
print('Number of books about HTTP: $itemCount.');
} else {
print('Request failed with status: ${response.statusCode}.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment