Skip to content

Instantly share code, notes, and snippets.

@mannuelf
Created July 18, 2022 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mannuelf/eaa635b275cb6ba2a282d3ec5022d220 to your computer and use it in GitHub Desktop.
Save mannuelf/eaa635b275cb6ba2a282d3ec5022d220 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