CodeBlanks Logo Icon CodeBlanks

GET

Learn GET requests with http.get.

GET requests

get(url) makes a GET request. url is a Uri. The returned Response's statusCode and body fields are read.

Example: var r = await get(Uri.parse('https://api.example.com/users')); print(r.statusCode); print(r.body);. 200 means success; body is JSON text.

GET requests carry no body; parameters go in the URL as a query string: Uri.parse('...?page=2').

Query strings

To add parameters to a URL, use the queryParameters field of Uri: Uri(scheme: 'https', host: 'api.example.com', path: '/users', queryParameters: {'page': '2'}).

This produces a query string like ?page=2. This structural method is preferred over manual string concatenation.

Multiple parameters are joined with & automatically; URL encoding is done automatically.

Practice Build a Uri with query

Inside main add var u = Uri(scheme: 'https', host: 'api.example.com', path: '/users', queryParameters: {'page': '2'});. Add print(u.host); and print(u.query);.

Starter code

main.dart
void main() {
  // build a URL with Uri constructor; add page=2 query
  // print host and query
}

Sample solution

main.dart
void main() {
  var u = Uri(
    scheme: 'https',
    host: 'api.example.com',
    path: '/users',
    queryParameters: {'page': '2'},
  );
  print(u.host);
  print(u.query);
}

Expected output

api.example.com
page=2

Questions and answers

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

1 GET call

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

Explanation: get(Uri) makes a GET request.

2 Does a GET request have a body?

  • Only form
  • Yes, always
  • Only JSON
  • No, parameters go in the URL Correct answer

Explanation: GET carries no body; parameters go in the URL as a query string.

3 Match the GET parts

Term Meaning
get(Uri) Request function
statusCode 200
body Response body
queryParameters URL parameters

All lessons in Advanced