CodeBlanks Logo Icon CodeBlanks

http package

Learn the HTTP client with the http package.

The http package

http is Dart's official HTTP client package. The get, post, put, delete functions make simple requests. Each call returns a Future<Response>.

A Response has three main parts: statusCode (int), body (String) and headers (Map). Example: var r = await get(url); print(r.statusCode);.

The package is imported from package:http/http.dart. A URL string is turned into a Uri via Uri.parse.

URLs with Uri.parse

http functions expect the URL as a Uri. Uri.parse('https://example.com/path?q=1') parses a URL string and splits it into structural parts.

Uri parts: .host (example.com), .path (/path), .query (q=1), .scheme (https). Each part can be read individually.

Example: var u = Uri.parse('https://api.example.com/users'); print(u.host); → 'api.example.com'. This is a deterministic way to analyze a URL statically.

Practice Parse a URL and print it

Inside main add var u = Uri.parse('https://api.example.com/users');. Add print(u.host); and print(u.path);.

Starter code

main.dart
void main() {
  // parse URL into Uri; print host and path
}

Sample solution

main.dart
void main() {
  var u = Uri.parse('https://api.example.com/users');
  print(u.host);
  print(u.path);
}

Expected output

api.example.com
/users

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does http.get return?

  • String
  • int
  • Future<Response> Correct answer
  • Stream

Explanation: get returns a Future<Response> asynchronously.

2 GET call

Question
var r = await ____(Uri.parse(url));
Answer
var r = await get(Uri.parse(url));

Explanation: get(Uri) makes a GET request.

3 Match the Uri parts

Term Meaning
.host Domain name
.path Path
.query Query
.scheme https

All lessons in Advanced