Skip to content

Instantly share code, notes, and snippets.

@timsneath
Last active April 6, 2018 16:00
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 timsneath/549f237af9529393fd4530155a4daa83 to your computer and use it in GitHub Desktop.
Save timsneath/549f237af9529393fd4530155a4daa83 to your computer and use it in GitHub Desktop.
Basic example of an async HTTP GET call to a JSON web service
import 'package:http/http.dart' as http;
import 'dart:convert';
const dadJokeApi = "https://icanhazdadjoke.com/";
const httpHeaders = const {
'User-Agent': 'DadJokes (https://github.com/timsneath/dadjokes)',
'Accept': 'application/json',
};
main() async {
final response = await http.read(dadJokeApi, headers: httpHeaders);
final decoded = json.decode(response);
if (decoded['status'] == 200) {
print (decoded['joke']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment