Last active
April 6, 2018 16:00
-
-
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
This file contains 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 '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