Created with <3 with dartpad.dev.
Created
July 18, 2022 07:36
-
-
Save mannuelf/eaa635b275cb6ba2a282d3ec5022d220 to your computer and use it in GitHub Desktop.
HTTP example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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