Back to results for “JSON”
Flutter › Networking with HTTP & Dio

What is the http package, and how does it let you actually make a genuinely basic GET request to fetch data from a remote server?

Beginner

Answer

The http package is Dart's own official, genuinely lightweight package specifically for making network requests, providing genuinely simple, straightforward methods such as get, post, put, and delete that each correspond directly to that exact same standard HTTP method, and making a genuinely basic GET request simply involves calling http.get with your particular target URL, then properly awaiting the resulting response, which conveniently includes the actual returned status code and the response body text.
final response = await http.get(Uri.parse('https://api.example.com/products'));
if (response.statusCode == 200) {
  final data = jsonDecode(response.body);
}
Real-world example A weather app makes a genuinely basic GET request using the http package to fetch the current weather forecast for a user's own specific location, checking the returned status code to properly confirm that particular request genuinely actually succeeded before attempting to parse its own returned response body.

Common follow-ups: What genuinely does a status code of 404 versus 500 actually specifically indicate about a given failed request?;What is the specific practical difference between the http package and the considerably more full featured Dio package?