ZetCode

Dart HTTP GET/POST request

last modified October 18, 2026

In this article we show how to send GET and POST requests in Dart with the http package.

HTTP

The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. HTTP protocol is the foundation of data communication for the World Wide Web.

In the code examples, we use httpbin.org, which is a freely available HTTP request and response service, and example.com, a tiny HTML page reserved for documentation.

HTTP GET

The HTTP GET method requests a representation of the specified resource. Requests using GET should only retrieve data.

HTTP POST

The HTTP POST method sends data to the server. It is often used when uploading a file or when submitting a completed web form.

Dart http

The http is a composable, Future-based library for making HTTP requests. We add the package to the project.

$ dart pub add http

The command adds the dependency to the pubspec.yaml file.

pubspec.yaml
name: app

environment:
  sdk: ^3.0.0

dependencies:
  http: ^1.2.0

Dart GET request

The following example creates a simple GET request in Dart.

main.dart
import 'package:http/http.dart' as http;

Future<String> fetchData() async {
  final resp = await http.get(Uri.https('example.com'));

  if (resp.statusCode == 200) {
    return resp.body;
  } else {
    throw Exception('Failed to fetch data: ${resp.statusCode}');
  }
}

void main() async {
  final data = await fetchData();
  print(data);
}

In the example, we send a GET request to example.com and print the response body.

import 'package:http/http.dart' as http;

The http package is imported.

Future<String> fetchData() async {

The fetchData is an asynchronous function which returns a future.

final resp = await http.get(Uri.https('example.com'));

A GET request is created with http.get. The await keyword gets the completed result of an asynchronous operation. The Uri.https constructor builds a URI with the https scheme; it is preferable to plain HTTP.

if (resp.statusCode == 200) {
  return resp.body;
} else {
  throw Exception('Failed to fetch data: ${resp.statusCode}');
}

If the response status code is a success, we return the body of the response. Otherwise, we throw an exception.

$ dart main.dart
<!doctype html><html lang=en><head><meta charset=utf-8><link rel=icon href=data:,><meta name=viewport content="width=device-width,initial-scale=1"><title>Example Domain</title>...

Dart GET with query parameters

Query parameters are passed in the queryParameters map of the Uri.https constructor. The values are URL-encoded automatically.

main.dart
import 'package:http/http.dart' as http;

void main() async {
  final uri = Uri.https('httpbin.org', '/get', {
    'name': 'John Doe',
    'occupation': 'gardener',
  });

  print(uri);

  final resp = await http.get(uri);

  print(resp.statusCode);
  print(resp.headers['content-type']);
  print(resp.body);
}

The httpbin.org/get endpoint echoes the request. We print the final URI, the status code, the Content-Type header, and the body. The response object also contains the headers and the raw bytes in bodyBytes.

$ dart main.dart
https://httpbin.org/get?name=John+Doe&occupation=gardener
200
application/json
{
  "args": {
    "name": "John Doe", 
    "occupation": "gardener"
  }, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Host": "httpbin.org", 
    "User-Agent": "Dart/3.13 (dart:io)"
  }, 
  "origin": "203.0.113.7", 
  "url": "https://httpbin.org/get?name=John+Doe&occupation=gardener"
}

Dart GET with user agent

The User-Agent request header is a string that lets servers and network peers identify the application, operating system, vendor, and/or version of the requesting user agent.

main.dart
import 'package:http/http.dart' as http;

void main() async {
  final resp = await http.get(
    Uri.https('httpbin.org', '/user-agent'),
    headers: {'User-Agent': 'Dart program'},
  );

  print(resp.body);
}

In the GET request, we include the User-Agent header. The /user-agent endpoint simply returns the User-Agent string that it received.

headers: {'User-Agent': 'Dart program'},

The headers named parameter of http.get is a map of request headers; here we set the User-Agent header.

$ dart main.dart
{
  "user-agent": "Dart program"
}

Dart HTTP POST request JSON data

The following example sends a POST request with data in JSON format.

main.dart
import 'dart:convert';

import 'package:http/http.dart' as http;

Future<http.Response> doPost() {
  return http.post(
    Uri.https('httpbin.org', '/post'),
    headers: {'Content-Type': 'application/json; charset=UTF-8'},
    body: jsonEncode({'name': 'John Doe', 'occupation': 'gardener'}),
  );
}

void main() async {
  final resp = await doPost();

  print(resp.statusCode);
  print(resp.body);
}

We generate a POST request to the httpbin.org/post endpoint. The data is encoded into JSON with jsonEncode.

return http.post(

A POST request is created with http.post.

headers: {'Content-Type': 'application/json; charset=UTF-8'},

We set the Content-Type to application/json.

body: jsonEncode({'name': 'John Doe', 'occupation': 'gardener'}),

The body is a JSON string created from a map.

$ dart main.dart
200
{
  "args": {}, 
  "data": "{\"name\":\"John Doe\",\"occupation\":\"gardener\"}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Content-Length": "43", 
    "Content-Type": "application/json; charset=UTF-8", 
    "Host": "httpbin.org", 
    "User-Agent": "Dart/3.13 (dart:io)"
  }, 
  "json": {
    "name": "John Doe", 
    "occupation": "gardener"
  }, 
  "origin": "203.0.113.7", 
  "url": "https://httpbin.org/post"
}

Dart HTTP POST request FORM data

With a POST form request, the data is URL-encoded in the request body. The Content-Type header is set to application/x-www-form-urlencoded. The keys and values are encoded in key-value tuples separated by &, with a = between the key and the value.

When the body is a Map<String, String>, the package encodes it as form data and sets the content type automatically.

main.dart
import 'package:http/http.dart' as http;

void main() async {
  final resp = await http.post(
    Uri.https('httpbin.org', '/post'),
    body: {'name': 'John Doe', 'occupation': 'gardener'},
  );

  print(resp.body);
}

We send a POST form request to httpbin.org. The map in the body parameter is URL-encoded by the library.

body: {'name': 'John Doe', 'occupation': 'gardener'},

The data is passed as a string map; the form field in the response shows how the server decoded it.

$ dart main.dart
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "name": "John Doe", 
    "occupation": "gardener"
  }, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Content-Length": "33", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "Dart/3.13 (dart:io)"
  }, 
  "json": null, 
  "origin": "203.0.113.7", 
  "url": "https://httpbin.org/post"
}

Dart reusing a Client

The top-level http.get and http.post functions open a new connection for each call. When we send several requests, it is more efficient to create one Client and close it when done.

main.dart
import 'package:http/http.dart' as http;

void main() async {
  final client = http.Client();

  try {
    for (final path in ['/status/200', '/status/404']) {
      final resp = await client
          .get(Uri.https('httpbin.org', path))
          .timeout(const Duration(seconds: 10));

      print('$path -> ${resp.statusCode} ${resp.reasonPhrase}');
    }
  } finally {
    client.close();
  }
}

The client is closed in the finally block. The timeout method of Future throws a TimeoutException when the server does not answer in time. Note that a 404 status does not throw; we must check statusCode ourselves.

$ dart main.dart
/status/200 -> 200 OK
/status/404 -> 404 NOT FOUND

Source

Dart http documentation

In this article we have created GET and POST requests in Dart.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Dart tutorials.